Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author : unlucky_13
- Problem_link :
- Category :
- Algorithm_Used :
- */
- #include<cstdio>
- #include<sstream>
- #include<cstdlib>
- #include<cctype>
- #include<cmath>
- #include<algorithm>
- #include<set>
- #include<queue>
- #include<stack>
- #include<list>
- #include<iostream>
- #include<fstream>
- #include<numeric>
- #include<string>
- #include<vector>
- #include<cstring>
- #include<map>
- #include<iterator>
- #define LL long long int
- const long long int inf = 2147483647 ;
- //const int minx=;
- //const int maxn=;
- using namespace std;
- int inter,f,dis[555][555],d[555],fire[555];
- struct data{
- int u,cost ;
- bool operator<(const data &p) const {
- return cost>p.cost ;
- }
- };
- vector<int>g[555] ;
- int Dijkstra(int s)
- {
- for(int i=1;i<=inter;i++) d[i]=inf ;
- priority_queue<data>pq;
- data u,v ;
- u.u = s ;
- u.cost = 0 ;
- d[s] = 0 ;
- pq.push(u) ;
- for(int i=1;i<=inter;i++){
- if(fire[i]==1) {
- d[i] = 0 ;
- u.u = i ;
- u.cost = 0 ;
- pq.push(u) ;
- }
- }
- while(!pq.empty()) {
- data u = pq.top() ;
- pq.pop() ;
- for(int i=0;i<g[u.u].size();i++){
- if(d[g[u.u][i]]>u.cost+dis[u.u][g[u.u][i]]){
- v.u = g[u.u][i] ;
- v.cost = u.cost+dis[u.u][g[u.u][i]] ;
- d[g[u.u][i]] = v.cost ;
- pq.push(v) ;
- }
- }
- }
- int ret = 0 ;
- for(int i=1;i<=inter;i++)
- {
- ret = max(ret,d[i]) ; //finding the maximum distance form the fire station
- }
- //cout<<s<<" returned "<<ret<<endl ;
- return ret ;
- }
- int main() {
- freopen("C:\\Users\\Mazhar\\Desktop\\Text_Files\\in.txt", "r", stdin);
- int tc ,x,y,cost;
- char line[1000] ;
- scanf("%d\n",&tc) ;
- while(tc--)
- {
- scanf("%d %d\n",&f,&inter) ;
- memset(fire,0,sizeof(fire)) ;
- memset(dis,0,sizeof(dis)) ;
- for(int i=1;i<=inter;i++) g[i].clear() ;
- for(int i=0;i<f;i++){
- scanf("%d",&x) ;
- fire[x] = 1 ;
- }
- gets(line) ;
- while(gets(line))
- {
- if(line[0]=='\0') break ;
- sscanf(line,"%d %d %d",&x,&y,&cost) ;
- //cout<<x<<" "<<y<<" "<<cost<<endl ;
- g[x].push_back(y) ;
- g[y].push_back(x) ;
- dis[x][y] = cost ;
- dis[y][x] = cost ;
- }
- //cout<<"-------------------"<<endl ;
- int MIN = inf ,res=-1 ;
- for(int i=1;i<=inter;i++){
- int DIS = Dijkstra(i) ;
- if(MIN>DIS) {
- res = i ;
- MIN = DIS ;
- }
- }
- if(res==-1){
- res = 1 ;
- while(fire[res]==1) res++ ;
- }
- cout<<res<<endl ;
- if(tc) cout<<endl ;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment