Advertisement
Josif_tepe

Untitled

Mar 14th, 2022
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include <set>
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10. #define pb push_back
  11. #define mp make_pair
  12. #define f first
  13. #define s second
  14.  
  15.  
  16. vector<pair<int, int>> graph[100005];
  17. long long dist[100005];
  18. bool visited[100005];
  19. int main()
  20. {
  21.     ios_base::sync_with_stdio(false);
  22.     cout.tie(0);
  23.     cin.tie(0);
  24. //    ifstream cin("in.txt");
  25.  
  26.     int n, m;
  27.     cin >> n >> m;
  28.     for(int i = 0; i < m; i++){
  29.         int a, b, c;
  30.         cin >> a >> b >> c;
  31.         graph[a-1].pb(mp(b-1, c));
  32.     }
  33.     for(int i =0 ; i < n; i++) {
  34.         dist[i] = 1e18;
  35.         visited[i] = false;
  36. //        sort(graph[i].begin(), graph[i].end(), [&](const pair<int, int> &a, const pair<int, int> &b) {
  37. //            return a.second < b.second;
  38. //        });
  39.     }
  40.  
  41.    
  42.     set<pair<int, long long> > pq;
  43.     dist[0] = 0;
  44.     pq.insert(make_pair(0, 0));
  45.     while(!pq.empty()){
  46.         pair<int, long long> c = *pq.begin();
  47.         pq.erase(pq.begin());
  48.         for(int i = 0; i < (int) graph[c.first].size(); i++){
  49.             int z = graph[c.first][i].f;
  50.             if(c.second + graph[c.first][i].second < dist[z]) {
  51.                 pq.erase(make_pair(z, dist[z]));
  52.                 dist[z] = c.second + graph[c.first][i].second;
  53.                 pq.insert(make_pair(z, dist[z]));
  54.                
  55.             }
  56.         }
  57.     }
  58.     for(int i = 0; i < n; i++){
  59.         cout << dist[i] << " ";
  60.     }
  61.    
  62.    
  63.     return 0;
  64.  
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement