nq1s788

дейкстра на сете

Jul 31st, 2025
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <deque>
  5. #include <map>
  6. #include <cmath>
  7.  
  8. #define se second
  9. #define fi first
  10. #define mp make_pair
  11. #define pb push_back
  12.  
  13. typedef long long ll;
  14. typedef long double ld;
  15.  
  16. using namespace std;
  17.  
  18. ll inf = (ll)1e9 + 5;
  19.  
  20. int main() {
  21.     int n, m;
  22.     cin >> n >> m;
  23.     vector<vector<pair<int, int>>> g(n);
  24.     while (m--) {
  25.         int x, y, w;
  26.         cin >> x >> y >> w;
  27.         x--, y--;
  28.         g[x].pb({y, w});
  29.         g[y].pb({x, w});
  30.     }
  31.     vector<int> w(n, inf);
  32.     w[0] = 0;
  33.     set<pair<int, int>> st;
  34.     st.insert({0, 0});
  35.     while (!st.empty()) {
  36.         pair<int, int> cur = *st.begin();
  37.         int h = cur.fi;
  38.         for (auto e : g[h]) {
  39.             if (w[e.se] > w[h] + e.fi) {
  40.                 w[e.se] = w[h] + e.fi;
  41.                 st.insert({w[e.se], e.se});
  42.             }
  43.         }
  44.     }
  45.     for (auto e : w) {
  46.         if (e == inf) cout << "-1" << ' ';
  47.         else cout << e << ' ';
  48.     }
  49.     return 0;
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment