unlucky_13

Untitled

May 30th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. /*
  2. Author : unlucky_13
  3. Problem_link :
  4. Category :
  5. Algorithm_Used :
  6. */
  7.  
  8. #include<cstdio>
  9. #include<sstream>
  10. #include<cstdlib>
  11. #include<cctype>
  12. #include<cmath>
  13. #include<algorithm>
  14. #include<set>
  15. #include<queue>
  16. #include<stack>
  17. #include<list>
  18. #include<iostream>
  19. #include<fstream>
  20. #include<numeric>
  21. #include<string>
  22. #include<vector>
  23. #include<cstring>
  24. #include<map>
  25. #include<iterator>
  26. #define LL long long int
  27. const long long int inf = 2147483647 ;
  28. //const int minx=;
  29. //const int maxn=;
  30.  
  31. using namespace std;
  32. int inter,f,dis[555][555],d[555],fire[555];
  33. struct data{
  34. int u,cost ;
  35. bool operator<(const data &p) const {
  36. return cost>p.cost ;
  37. }
  38. };
  39. vector<int>g[555] ;
  40.  
  41. int Dijkstra(int s)
  42. {
  43.  
  44. for(int i=1;i<=inter;i++) d[i]=inf ;
  45. priority_queue<data>pq;
  46. data u,v ;
  47. u.u = s ;
  48. u.cost = 0 ;
  49. d[s] = 0 ;
  50. pq.push(u) ;
  51. for(int i=1;i<=inter;i++){
  52. if(fire[i]==1) {
  53. d[i] = 0 ;
  54. u.u = i ;
  55. u.cost = 0 ;
  56. pq.push(u) ;
  57. }
  58. }
  59.  
  60.  
  61.  
  62. while(!pq.empty()) {
  63. data u = pq.top() ;
  64. pq.pop() ;
  65. for(int i=0;i<g[u.u].size();i++){
  66.  
  67.  
  68. if(d[g[u.u][i]]>u.cost+dis[u.u][g[u.u][i]]){
  69. v.u = g[u.u][i] ;
  70. v.cost = u.cost+dis[u.u][g[u.u][i]] ;
  71. d[g[u.u][i]] = v.cost ;
  72. pq.push(v) ;
  73. }
  74. }
  75. }
  76.  
  77. int ret = 0 ;
  78.  
  79. for(int i=1;i<=inter;i++)
  80. {
  81. ret = max(ret,d[i]) ; //finding the maximum distance form the fire station
  82.  
  83. }
  84.  
  85. //cout<<s<<" returned "<<ret<<endl ;
  86. return ret ;
  87.  
  88. }
  89.  
  90.  
  91. int main() {
  92.  
  93. freopen("C:\\Users\\Mazhar\\Desktop\\Text_Files\\in.txt", "r", stdin);
  94.  
  95. int tc ,x,y,cost;
  96. char line[1000] ;
  97. scanf("%d\n",&tc) ;
  98.  
  99. while(tc--)
  100. {
  101.  
  102. scanf("%d %d\n",&f,&inter) ;
  103. memset(fire,0,sizeof(fire)) ;
  104. memset(dis,0,sizeof(dis)) ;
  105. for(int i=1;i<=inter;i++) g[i].clear() ;
  106.  
  107. for(int i=0;i<f;i++){
  108. scanf("%d",&x) ;
  109. fire[x] = 1 ;
  110. }
  111.  
  112. gets(line) ;
  113. while(gets(line))
  114. {
  115.  
  116. if(line[0]=='\0') break ;
  117. sscanf(line,"%d %d %d",&x,&y,&cost) ;
  118. //cout<<x<<" "<<y<<" "<<cost<<endl ;
  119. g[x].push_back(y) ;
  120. g[y].push_back(x) ;
  121. dis[x][y] = cost ;
  122. dis[y][x] = cost ;
  123.  
  124. }
  125. //cout<<"-------------------"<<endl ;
  126. int MIN = inf ,res=-1 ;
  127. for(int i=1;i<=inter;i++){
  128. int DIS = Dijkstra(i) ;
  129. if(MIN>DIS) {
  130. res = i ;
  131. MIN = DIS ;
  132. }
  133. }
  134. if(res==-1){
  135. res = 1 ;
  136. while(fire[res]==1) res++ ;
  137. }
  138. cout<<res<<endl ;
  139. if(tc) cout<<endl ;
  140.  
  141.  
  142. }
  143.  
  144.  
  145.  
  146. return 0;
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment