Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define maxm 3001
  3.  
  4. using namespace std;
  5.  
  6. int t, n, m, u, v, c, s;
  7. vector<pair<int, int> > G[maxm];
  8.  
  9. void shortestReach() {
  10. vector<int> D(n+1, INT_MAX);
  11. D[s] = 0;
  12. priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int> > > pq;
  13. pq.push(make_pair(0, s));
  14. while(!pq.empty()){
  15. int u = pq.top().second;
  16. pq.pop();
  17. for(int i=0; i<G[u].size(); ++i){
  18. int v = G[u][i].first;
  19. int cost = G[u][i].second;
  20. if(D[v] > D[u] + cost){
  21. D[v] = D[u] + cost;
  22. pq.push(make_pair(D[v], v));
  23. }
  24. }
  25. }
  26. for(int i=1; i<=n; ++i)
  27. if(i != s)
  28. if(D[i] == INT_MAX)
  29. cout<<"-1 ";
  30. else
  31. cout<<D[i]<<" ";
  32. cout<<"\n";
  33. }
  34.  
  35. int main()
  36. {
  37. ios_base::sync_with_stdio(false);
  38. cin >> t;
  39. while(t--){
  40. cin>>n>>m;
  41. for(int i=0; i<m; ++i){
  42. cin>>u>>v>>c;
  43. G[u].push_back(make_pair(v, c));
  44. G[v].push_back(make_pair(u, c));
  45. }
  46. cin>>s;
  47. shortestReach();
  48. for(int i=1; i<=n; ++i)
  49. G[i].clear();
  50. }
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement