Advertisement
Shiam7777777

Untitled

Feb 1st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  4. #define ll long long
  5. #define ld double
  6. #define llu long long unsigned
  7.  
  8. # define INF 0x3f3f3f3f
  9.  
  10. typedef pair<int, int> iPair;
  11.  
  12. class Graph
  13. {
  14.     int V;
  15.  
  16.     list< pair<int, int> > *adj;
  17.  
  18. public:
  19.     Graph(int V);
  20.  
  21.     void addEdge(int u, int v, int w);
  22.  
  23.     void shortestPath(int s);
  24. };
  25.  
  26. Graph::Graph(int V)
  27. {
  28.     this->V = V;
  29.     adj = new list<iPair> [V];
  30. }
  31.  
  32. void Graph::addEdge(int u, int v, int w)
  33. {
  34.         adj[u].push_back(make_pair(v, w));
  35.         adj[v].push_back(make_pair(u, w));
  36. }
  37.  
  38. void Graph::shortestPath(int src)
  39. {
  40.     priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
  41.  
  42.     vector<int> dist(V, INF);
  43.  
  44.     pq.push(make_pair(0, src));
  45.     dist[src] = 0;
  46.  
  47.     while (!pq.empty())
  48.     {
  49.         int u = pq.top().second;
  50.         pq.pop();
  51.  
  52.         list< pair<int, int> >::iterator i;
  53.         for (i = adj[u].begin(); i != adj[u].end(); ++i)
  54.         {
  55.  
  56.             int v = (*i).first;
  57.             int weight = (*i).second;
  58.  
  59.             if (dist[v] > max(dist[u] , weight) )
  60.             {
  61.                 dist[v] = max(dist[u] , weight);
  62.                 pq.push(make_pair(dist[v], v));
  63.             }
  64.         }
  65.     }
  66.  
  67.     for (int i = 0; i < V; ++i)
  68.     {
  69.         if( dist[i] > 20001 )
  70.             cout<<"Impossible\n";
  71.         else
  72.             printf("%d\n", dist[i]);
  73.     }
  74.  
  75. }
  76.  
  77. int main()
  78. {
  79.     fast;
  80.     int t;
  81.     cin>>t;
  82.     int count = 0;
  83.     for( count = 1 ; count <= t ; count++ )
  84.     {
  85.         cout<<"Case "<<count<<": "<<'\n';
  86.         int n , e;
  87.         cin>>n>>e;
  88.         Graph g(n);
  89.         while( e-- )
  90.         {
  91.             int u , v , w;
  92.             cin>>u>>v>>w;
  93.             g.addEdge(u, v, w);
  94.         }
  95.         int x;
  96.         cin>>x;
  97.  
  98.         g.shortestPath( x );
  99.  
  100.     }
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement