Advertisement
Saleh127

Light OJ 1099 / Dijkstra - 2nd shortest path

Mar 17th, 2022
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. /***
  2.  created: 2022-03-17-15.27.40
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11. vector<pair<ll,ll>>g[50000];
  12.  
  13. ll dis[5015],cst[5010];
  14.  
  15.  
  16. void clr(ll n)
  17. {
  18.     for(ll i=0;i<n;i++)
  19.     {
  20.         g[i].clear();
  21.         cst[i]=dis[i]=INT_MAX;
  22.     }
  23. }
  24.  
  25. int main()
  26. {
  27.     ios_base::sync_with_stdio(0);
  28.     cin.tie(0);
  29.     cout.tie(0);
  30.  
  31.     test
  32.     {
  33.         ll n,m,i,j,k,l;
  34.  
  35.         cin>>n>>m;
  36.  
  37.         clr(n+5);
  38.  
  39.         priority_queue<pair<ll,ll>>q;
  40.  
  41.  
  42.         for(i=0;i<m;i++)
  43.         {
  44.             cin>>j>>k>>l;
  45.             g[j].push_back({k,l});
  46.             g[k].push_back({j,l});
  47.         }
  48.  
  49.         q.push({0,1});
  50.         cst[1]=0;
  51.  
  52.  
  53.         while(q.empty()==false)
  54.         {
  55.             auto d=q.top();
  56.             q.pop();
  57.  
  58.             if(dis[d.second]<(-1*d.first)) continue;
  59.  
  60.             for(auto x:g[d.second])
  61.             {
  62.                 ll u= x.first;
  63.  
  64.                 ll val= x.second + (-1*d.first);
  65.  
  66.                 if(val<cst[u])
  67.                 {
  68.                     swap(cst[u],val);
  69.                     q.push({-cst[u],u});
  70.                 }
  71.                
  72.                 if(val<dis[u] && val!=cst[u])
  73.                 {
  74.                     dis[u]=val;
  75.                     q.push({-dis[u],u});
  76.                 }
  77.             }
  78.  
  79.         }
  80.  
  81.         cout<<"Case "<<cs<<": "<<dis[n]<<nl;
  82.     }
  83.  
  84.     get_lost_idiot;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement