Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxN = 510;
- vector<pair<int,int>> adj[maxN];
- int dist[maxN];
- void dijkstra (int source,int n) {
- for (int i = 0; i < n; ++i) dist[i] = (int) 1e7;
- dist[source] = 0;
- priority_queue<pair<int,int>> pq;
- pq.push(make_pair(0,source));
- while (!pq.empty()) {
- int node = pq.top().second;
- int node_cost = abs(pq.top().first);
- pq.pop();
- if (dist[node] < node_cost) continue;
- for (pair<int,int> child : adj[node]) {
- if (dist[child.first] > max(child.second,node_cost)) {
- dist[child.first] = max(child.second,node_cost);
- pq.push(make_pair(-dist[child.first],child.first));
- }
- }
- }
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T;
- cin >> T;
- for (int test_case = 1; test_case <= T; ++test_case) {
- int n,m;
- cin >> n >> m;
- for (int i = 0; i < n; ++i) adj[i].clear();
- for (int i = 1; i <= m; ++i) {
- int u,v,c;
- cin >> u >> v >> c;
- adj[u].push_back(make_pair(v,c));
- adj[v].push_back(make_pair(u,c));
- }
- int source;
- cin >> source;
- dijkstra (source,n);
- //for (int i = 0; i < n; ++i) cout << max_cost[i] << ' ';
- cout << "Case " << test_case << ":\n";
- for (int i = 0; i < n; ++i) cout << (dist[i] == (int) 1e7 ? "Impossible" : to_string(dist[i])) << '\n';
- }
- //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement