unlucky_13

LOJ_1002 - Country Roads

Jul 24th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. //============================================================================
  2. // code written by unlucky_13
  3. //============================================================================
  4.  
  5. #include <iostream>
  6. #include<cstdio>
  7. #include<cstdlib>
  8. #include<cstring>
  9. #include<cmath>
  10. #include<algorithm>
  11. #include<vector>
  12. #include<queue>
  13. #include<stack>
  14. #include<set>
  15. #include<list>
  16. #include<sstream>
  17. #include<fstream>
  18. #define INF 1<<29
  19.  
  20. using namespace std;
  21.  
  22. int n,m ;
  23.  
  24.  
  25. struct data
  26. {  int city,cost ;
  27. bool taken ;
  28.     bool operator < ( const data& p ) const {
  29.         return cost > p.cost;
  30.     }
  31. };
  32.  
  33. vector<data>edg[500] ;
  34. int dis[500+10] ;
  35.  
  36.  
  37.  
  38. void dijkstra(int source){
  39.  
  40.  
  41.     for(int i=0;i<n;i++) dis[i] = INF ;
  42.     dis[source] = 0 ;
  43.     priority_queue<data>pq ;
  44.     data p,q ;
  45.     p.city = source ;
  46.     p.cost = 0 ;
  47.     pq.push(p) ;
  48.     while(!pq.empty()){
  49.  
  50.         p = pq.top() ; pq.pop() ;
  51.  
  52.  
  53.         for(int i=0;i<edg[p.city].size();i++) {
  54.  
  55.             q = edg[p.city][i] ;
  56.  
  57.             if(dis[q.city]>max(dis[p.city],q.cost)){
  58.                 pq.push(edg[p.city][i]) ;
  59.                 dis[q.city] = max(dis[p.city],q.cost) ;
  60.             }
  61.         }
  62.  
  63.     }
  64. }
  65.  
  66.  
  67.  
  68. int main() {
  69.     //freopen("d:\\in.txt","r",stdin) ;
  70.  
  71.     int t,u,v,cost,caseno=0,testcase ;
  72.     data G ;
  73.     scanf("%d",&testcase) ;
  74.     while(testcase--){
  75.         scanf("%d %d",&n,&m) ;
  76.         for(int i=0;i<=500;i++) edg[i].clear() ;
  77.         for(int i=0;i<m;i++){
  78.         scanf("%d %d %d",&u,&v,&cost) ;
  79.         G.city = u ; G.cost = cost ; G.taken = false ;
  80.         edg[v].push_back(G) ;
  81.         G.city = v ;
  82.         edg[u].push_back(G) ;
  83.         }
  84.         scanf("%d",&t) ;
  85.         dijkstra(t) ;
  86.         printf("Case %d:\n",++caseno) ;
  87.         for(int i=0;i<n;i++){
  88.             if(dis[i]==INF) cout<<"Impossible"<<endl ;
  89.             else cout<<dis[i]<<endl ;
  90.         }
  91.     }
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment