Advertisement
Saleh127

UVA 10986 / Dijkstra Basic

Jun 20th, 2022
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. /***
  2.  created: 2022-06-20-16.28.23
  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[200005];
  17.  
  18. int main()
  19. {
  20.     ios_base::sync_with_stdio(0);
  21.     cin.tie(0);
  22.     cout.tie(0);
  23.  
  24.  
  25.     test
  26.     {
  27.         ll n,m,i,j,k,l,s,d;
  28.  
  29.         cin>>n>>m>>s>>d;
  30.  
  31.  
  32.         ll cost[n+5];
  33.  
  34.         fill(cost,cost+n+2,1e17);
  35.  
  36.  
  37.         while(m--)
  38.         {
  39.             ll a,b,c;
  40.  
  41.             cin>>a>>b>>c;
  42.  
  43.             g[a].push_back({b,c});
  44.             g[b].push_back({a,c});
  45.         }
  46.  
  47.         cost[s]=0;
  48.  
  49.         priority_queue<ll>q;
  50.  
  51.         q.push(s);
  52.  
  53.         while(q.empty()==false)
  54.         {
  55.             ll u=q.top();
  56.             q.pop();
  57.  
  58.             for(auto i:g[u])
  59.             {
  60.                 if(cost[i.first]> cost[u]+i.second)
  61.                 {
  62.                     cost[i.first]=cost[u]+i.second;
  63.                     q.push(i.first);
  64.                 }
  65.             }
  66.         }
  67.  
  68.         cout<<"Case #"<<cs<<": ";
  69.  
  70.         if(cost[d]==(1e17)) cout<<"unreachable"<<nl;
  71.         else cout<<cost[d]<<nl;
  72.  
  73.         for(i=0;i<n;i++) g[i].clear();
  74.  
  75.     }
  76.  
  77.  
  78.     get_lost_idiot;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement