Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fast()(ios_base::sync_with_stdio(false),cin.tie(NULL));
- int const M= 1e9+1;
- int const N= 1e4+1;
- bool vis[N];
- int cost[N];
- vector<vector<pair<int,int> > > root;
- void dijkstra(int src)
- {
- int v,c;
- priority_queue<pair<int,int> >q;
- cost[src]=0;
- q.push(make_pair(0,src));
- while(!q.empty())
- {
- v=q.top().second;
- c=-q.top().first;
- q.pop();
- if(vis[v])continue;
- vis[v]=true;
- for(auto pr: root[v])
- {
- if(pr.second+c<cost[pr.first])
- {
- cost[pr.first]=pr.second+c;
- q.push(make_pair(-(pr.second+c),pr.first));
- }
- }
- }
- }
- int main()
- {
- int t;
- scanf("%d",&t);
- while(t--)
- {
- int node, edge;
- scanf("%d %d",&node,&edge);
- root.clear();
- root.resize(node);
- int u,v,w;
- for(int i=0;i<=node;i++)cost[i]=M;
- memset(vis,false,sizeof(vis));
- for(int i=0;i<edge;i++)
- {
- scanf("%d %d %d",&u,&v,&w);
- u--,v--;
- root[u].push_back(make_pair(v,w));
- }
- scanf("%d %d",&u,&v);
- u--,v--;
- dijkstra(u);
- if(cost[v]==M)printf("NO\n");
- else printf("%d\n",cost[v]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement