Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
100
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. #define int long long
  3. #define pii pair<long long,long long>
  4.  
  5. using namespace std;
  6. const int N = 1e5 + 1;
  7.  
  8. vector<pii> gr[N];
  9. int dist[N];
  10. int32_t main() {
  11.     int n, m;
  12.     ios_base::sync_with_stdio(0);
  13.     cin.tie(0);
  14.     freopen("pathbgep.in", "r", stdin);
  15.     freopen("pathbgep.out", "w", stdout);
  16.     cin >> n >> m;
  17.     for (int i = 0 ; i < m; ++ i) {
  18.         int a, b, c;
  19.         cin >> a >> b >> c;
  20.         -- a; -- b;
  21.         gr[a].push_back({b, c});
  22.         gr[b].push_back({a, c});
  23.     }
  24.     for (int i = 0; i < n; ++ i) {
  25.         dist[i] = LLONG_MAX;
  26.     }
  27.     priority_queue<pii, vector<pii>, greater<pii> > pq;
  28.     dist[0] = 0;
  29.     pq.push({0, 0});
  30.     while (!pq.empty()) {
  31.         pii cur = pq.top();
  32.         pq.pop();
  33.         int dst = cur.first, v = cur.second;
  34.         if (dst > dist[v]) {
  35.             continue;
  36.         }
  37.         for (pii i : gr[v]) {
  38.             int dst_ = dst + i.second;
  39.             if (dst_ < dist[i.first]) {
  40.                 dist[i.first] = dst_;
  41.                 pq.push({dst_, i.first});
  42.             }
  43.         }
  44.     }
  45.     for (int i = 0; i < n; ++ i) {
  46.         cout << dist[i] << ' ';
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement