Advertisement
newb_ie

LightOJ - Country Roads

Mar 13th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 510;
  5. vector<pair<int,int>> adj[maxN];
  6. int dist[maxN];
  7.  
  8. void dijkstra (int source,int n) {
  9.     for (int i = 0; i < n; ++i) dist[i] = (int) 1e7;
  10.     dist[source] = 0;
  11.     priority_queue<pair<int,int>> pq;
  12.     pq.push(make_pair(0,source));
  13.     while (!pq.empty()) {
  14.         int node = pq.top().second;
  15.         int node_cost = abs(pq.top().first);
  16.         pq.pop();
  17.         if (dist[node] < node_cost) continue;
  18.         for (pair<int,int> child : adj[node]) {
  19.             if (dist[child.first] > max(child.second,node_cost)) {
  20.                 dist[child.first] = max(child.second,node_cost);
  21.                 pq.push(make_pair(-dist[child.first],child.first));
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. int main () {
  28.      ios::sync_with_stdio(false);
  29.      cin.tie(nullptr);
  30.      cout.tie(nullptr);
  31.      int T;
  32.      cin >> T;
  33.      for (int test_case = 1; test_case <= T; ++test_case) {
  34.          int n,m;
  35.          cin >> n >> m;
  36.          for (int i = 0; i < n; ++i) adj[i].clear();
  37.          for (int i = 1; i <= m; ++i) {
  38.              int u,v,c;
  39.              cin >> u >> v >> c;
  40.              adj[u].push_back(make_pair(v,c));
  41.              adj[v].push_back(make_pair(u,c));
  42.          }
  43.          int source;
  44.          cin >> source;
  45.          dijkstra (source,n);
  46.          //for (int i = 0; i < n; ++i) cout << max_cost[i] << ' ';
  47.          cout << "Case " << test_case << ":\n";
  48.          for (int i = 0; i < n; ++i) cout << (dist[i] == (int) 1e7 ? "Impossible" : to_string(dist[i])) << '\n';
  49.      }
  50.      //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement