Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 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.         for(auto it: v[node])
  48.         {
  49.             if(!bo[it.first])
  50.             {
  51.                 bo[it.first] = true;
  52.  
  53.                 if(dist[it.first] > dist[node] + it.second)
  54.                     dist[it.first] = dist[node] + it.second, pq.push(it.first);
  55.                
  56.             }
  57.            
  58.         }
  59.     }
  60. }
  61. int main()
  62. {
  63.     szybcior;
  64.     init();
  65.     dijkstra(1);
  66.     // cout << "\n";
  67.    
  68.     for(ll i = 1; i <= n; i++)
  69.     {
  70.         if(dist[i] != inf)
  71.             cout << dist[i] << " ";
  72.         else
  73.             cout << "-1 ";
  74.     }
  75.     return 0;  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement