Saleh127

Light oj(LO) 1019

Jan 18th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. vector<pair<ll,ll>>g[10000];
  6. ll dis[10000];
  7. int main()
  8. {
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0);cout.tie(0);
  11.  
  12. test
  13. {
  14. ll n,m,i,j,k,l,a,b,c;
  15. cin>>n>>m;
  16. for(i=0;i<n+5;i++)
  17. {
  18. g[i].clear();
  19. dis[i]=10000000000;
  20. }
  21. for(i=0;i<m;i++)
  22. {
  23. cin>>a>>b>>c;
  24. g[a].push_back({b,c});
  25. g[b].push_back({a,c});
  26. }
  27.  
  28. dis[1]=0;
  29. priority_queue<ll>q;
  30. q.push(1);
  31.  
  32. while(q.empty()==false)
  33. {
  34. ll u=q.top();
  35. q.pop();
  36.  
  37. for(auto x:g[u])
  38. {
  39. if(dis[x.first]>dis[u]+x.second)
  40. {
  41. dis[x.first]=dis[u]+x.second;
  42. q.push(x.first);
  43. }
  44. }
  45. }
  46. cout<<"Case "<<cs<<": ";
  47.  
  48. if(dis[n]==10000000000) cout<<"Impossible"<<endl;
  49. else cout<<dis[n]<<endl;
  50.  
  51. }
  52. return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment