Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define ll long long
- #define ld double
- #define llu long long unsigned
- # define INF 0x3f3f3f3f
- typedef pair<int, int> iPair;
- class Graph
- {
- int V;
- list< pair<int, int> > *adj;
- public:
- Graph(int V);
- void addEdge(int u, int v, int w);
- void shortestPath(int s);
- };
- Graph::Graph(int V)
- {
- this->V = V;
- adj = new list<iPair> [V];
- }
- void Graph::addEdge(int u, int v, int w)
- {
- adj[u].push_back(make_pair(v, w));
- adj[v].push_back(make_pair(u, w));
- }
- void Graph::shortestPath(int src)
- {
- priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
- vector<int> dist(V, INF);
- pq.push(make_pair(0, src));
- dist[src] = 0;
- while (!pq.empty())
- {
- int u = pq.top().second;
- pq.pop();
- list< pair<int, int> >::iterator i;
- for (i = adj[u].begin(); i != adj[u].end(); ++i)
- {
- int v = (*i).first;
- int weight = (*i).second;
- if (dist[v] > max(dist[u] , weight) )
- {
- dist[v] = max(dist[u] , weight);
- pq.push(make_pair(dist[v], v));
- }
- }
- }
- for (int i = 0; i < V; ++i)
- {
- if( dist[i] > 20001 )
- cout<<"Impossible\n";
- else
- printf("%d\n", dist[i]);
- }
- }
- int main()
- {
- fast;
- int t;
- cin>>t;
- int count = 0;
- for( count = 1 ; count <= t ; count++ )
- {
- cout<<"Case "<<count<<": "<<'\n';
- int n , e;
- cin>>n>>e;
- Graph g(n);
- while( e-- )
- {
- int u , v , w;
- cin>>u>>v>>w;
- g.addEdge(u, v, w);
- }
- int x;
- cin>>x;
- g.shortestPath( x );
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement