Advertisement
Rentib

Untitled

Feb 14th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. ios_base::sync_with_stdio(0);
  5. cin.tie(0);
  6. cout.tie(0);
  7. int n, m, v;
  8. long long dist;
  9. cin >> n >> m;
  10. vector<pair<int, int>> G[n + 1];
  11. vector<long long> odl(n + 1, LLONG_MAX);
  12. for(int i = 0, a, b, c;i < m;i++){
  13. cin >> a >> b >> c;
  14. G[b].emplace_back(a, c);
  15. }
  16. priority_queue<pair<long long, int>> q;
  17. q.emplace(0, 1);
  18. while(!q.empty()){
  19. tie(dist, v) = q.top();
  20. q.pop();
  21. if(-dist > odl[v])
  22. continue;
  23. odl[v] = -dist;
  24. for(auto i : G[v])
  25. if(odl[i.first] > -dist + i.second)
  26. q.emplace(dist - i.second, i.first);
  27. }
  28. for(int i = 1;i <= n;i++){
  29. if(odl[i] == LLONG_MAX)
  30. cout << "+oo\n";
  31. else
  32. cout << odl[i] << '\n';
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement