Advertisement
Saleh127

Codeforces 1725M / Dijkstra Variant

Sep 4th, 2022
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. /***
  2.  created: 2022-09-05-02.38.26
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16. vector<pair<ll,ll>>g[100005],gr[100005];
  17.  
  18. ll cost[100005];
  19.  
  20. int main()
  21. {
  22.     ios_base::sync_with_stdio(0);
  23.     cin.tie(0);
  24.     cout.tie(0);
  25.  
  26.     ll n,m,i,j,k,l;
  27.  
  28.     cin>>n>>m;
  29.  
  30.     for(i=0;i<m;i++)
  31.     {
  32.         ll u,v,w;
  33.         cin>>u>>v>>w;
  34.         g[u].push_back({v,w});
  35.         gr[v].push_back({u,w});
  36.     }
  37.  
  38.     //solution -> -> -> <- <- <- (max 1 reverse)
  39.  
  40.     fill(cost,cost+100002,1e17);
  41.  
  42.     priority_queue <pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> q;
  43.  
  44.     q.push({0,1});
  45.  
  46.     cost[1]=0;
  47.  
  48.     while(!q.empty())
  49.     {
  50.         auto d=q.top();
  51.         q.pop();
  52.  
  53.         if(cost[d.second]<d.first) continue;
  54.  
  55.         for(auto u:g[d.second])
  56.         {
  57.             if(cost[u.first]> u.second+d.first)
  58.             {
  59.                 cost[u.first]=u.second+d.first;
  60.                 q.push({cost[u.first],u.first});
  61.             }
  62.         }
  63.     }
  64.  
  65.     for(i=1;i<=n;i++)
  66.     {
  67.         q.push({cost[i],i});
  68.     }
  69.  
  70.     //reverse move
  71.    
  72.     while(!q.empty())
  73.     {
  74.         auto d=q.top();
  75.         q.pop();
  76.  
  77.         if(cost[d.second]<d.first) continue;
  78.  
  79.         for(auto u:gr[d.second])
  80.         {
  81.             if(cost[u.first]> u.second+d.first)
  82.             {
  83.                 cost[u.first]=u.second+d.first;
  84.                 q.push({cost[u.first],u.first});
  85.             }
  86.         }
  87.     }
  88.  
  89.     for(i=2;i<=n;i++)
  90.     {
  91.         if(cost[i]==1e17) cout<<-1<<" ";
  92.         else cout<<cost[i]<<" ";
  93.     }
  94.  
  95.     cout<<nl;
  96.  
  97.     get_lost_idiot;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement