Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cmath>
  6. #include <stack>
  7. #include <iomanip>
  8. #include <bitset>
  9.  
  10. #define szybcior ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  11.  
  12. using namespace std;
  13. typedef long long ll;
  14. typedef long double ld;
  15. const ll inf = 10e17;
  16. const ll ile = 300090;
  17. vector<pair<ll,ll>> v[ile];
  18. vector<bool> bo(ile, 0);
  19. ll dist[ile];
  20. ll n,m;
  21.  
  22. inline void init()
  23. {
  24.    
  25.     cin >> n >> m;
  26.     for(int i = 0; i < m; i++)
  27.     {
  28.         ll a,b,c;
  29.         cin >> a >> b >> c;
  30.         v[a].push_back({b,c});
  31.         v[b].push_back({a,c});
  32.     }
  33.  
  34.     for(int i = 0; i <= n + 1; i++)
  35.         dist[i] = inf;
  36. }
  37.  
  38. inline void dijkstra(ll start)
  39. {
  40.     priority_queue<ll, std::vector<ll>, std::greater<ll> > pq;
  41.     pq.push(start);
  42.     dist[start] = 0;
  43.     while(!pq.empty())
  44.     {
  45.         ll node = pq.top();
  46.         pq.pop();
  47.         if(!bo[node])
  48.         {  
  49.             for(auto it: v[node])
  50.                 if(dist[it.first] > dist[node] + it.second)
  51.                     dist[it.first] = dist[node] + it.second, pq.push(it.first);
  52.  
  53.             bo[node] = true;
  54.         }
  55.     }
  56. }
  57. int main()
  58. {
  59.     szybcior;
  60.     init();
  61.     dijkstra(1);
  62.     // cout << "\n";
  63.    
  64.     for(ll i = 2; i <= n; i++)
  65.     {
  66.         if(dist[i] != inf)
  67.             cout << dist[i] << " ";
  68.         else
  69.             cout << "-1 ";
  70.     }
  71.     return 0;  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement