Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<utility>
  5. #include<queue>
  6. #include<climits>
  7. using namespace std;
  8. typedef pair<int,int> pi;
  9. typedef vector<pi> pii;
  10. main()
  11. {
  12.     int t;
  13.     scanf("%d",&t);
  14.     while(t--)
  15.     {
  16.         int n,high,from,to;
  17.         scanf("%d%d%d%d",&n,&high,&from,&to);
  18.         vector<pii> adj(n+1);
  19.         for(int i=0;i<high;i++)
  20.         {
  21.             int s,e,dist;
  22.             scanf("%d%d%d",&s,&e,&dist);
  23.             adj[s].push_back(pi(e,dist));
  24.         }
  25.         vector<int> dist(n+1,INT_MAX);
  26.         dist[from]=0;
  27.         priority_queue< pi,pii,greater<pi> > q;
  28.         q.push(pi(0,from));
  29.         while(!q.empty())
  30.         {
  31.             pi my=q.top();
  32.             q.pop();
  33.             int cv=my.second,cd=my.first;
  34.             if(cv==to)
  35.                 break;
  36.             if(cd<=dist[cv])
  37.             {
  38.                 for(int i=0;i<adj[cv].size();i++)
  39.                 {
  40.                     int nv=adj[cv][i].second,nd=adj[cv][i].first;
  41.                     if(dist[nv]>nd+dist[cv])
  42.                     {
  43.                         dist[nv]=nd+dist[cv];
  44.                         q.push(pi(dist[nv],nv));
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         for(int i=0;i<dist.size();i++)
  50.             cout<<dist[i]<<" ";
  51.         if(dist[to]==INT_MAX)
  52.             cout<<"NONE\n";
  53.         else
  54.             cout<<dist[to]<<"\n";
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement