Advertisement
Rentib

Untitled

Feb 14th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. template <typename T> T getczary(){//magia!
  4. int ujemna = false, znak = getchar_unlocked();
  5. T wynik = (T)0;
  6. while(!isdigit(znak)){
  7. if(znak == '-')
  8. ujemna = true;
  9. znak = getchar_unlocked();
  10. }
  11. while(isdigit(znak)){
  12. wynik *= 10;
  13. wynik += znak - '0';
  14. znak = getchar_unlocked();
  15. }
  16. if(ujemna)
  17. wynik *= -1;
  18. return wynik;
  19. }
  20. priority_queue<pair<int, int>> q;
  21. vector<int> odl(100007, INT_MAX);
  22. vector<pair<int, int>> G[100007];
  23. int main(){
  24. ios_base::sync_with_stdio(0);
  25. cin.tie(0);
  26. cout.tie(0);
  27. int n, m, v, dist;
  28. n = getczary<int>();
  29. m = getczary<int>();
  30. for(int i = 0, a, b, c;i < m;i++){
  31. a = getczary<int>();
  32. b = getczary<int>();
  33. c = getczary<int>();
  34. G[b].emplace_back(a, c);
  35. }
  36. q.emplace(0ll, 1);
  37. while(!q.empty()){
  38. tie(dist, v) = q.top();
  39. q.pop();
  40. if(-dist > odl[v])
  41. continue;
  42. odl[v] = -dist;
  43. for(auto i : G[v])
  44. if(odl[i.first] > -dist + i.second)
  45. q.emplace(dist - i.second, i.first);
  46. }
  47. for(int i = 1;i <= n;i++){
  48. if(odl[i] != INT_MAX)
  49. cout << odl[i] << '\n';
  50. else
  51. cout << "+oo\n";
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement