Title Description:
Farmer John was surprised to find some wormholes when he explored the farm, a special thing, a one-way tunnel, he could reach the other end of the wormhole, and it could traverse the time before it arrived. Farmer John is made up of N farms, numbered 1-n, M-one-way channels, and W wormholes.
Farmer John is a time-space fan, and he wants to do something: start on some farms, travel through wormholes or roads, and pass through wormholes to get back to him.
Help Farmer John If he has the possibility of crossing to the previous time, he will give you a complete map, he has f a farm, the length of the passage is less than 10,000 seconds, and the wormhole crossing is not more than 10,000 seconds
Input:
F represents the number of test instances
Second row three integers N, M, W separated by a space
The next M-line is M-Road, and the road is bidirectional. S, E, T represents the time from S to E for T.
The next W line represents the W wormhole, the wormhole is unidirectional, S, E, T, and the time from S through E is T.
Topic Analysis:
He asked if you can cross, in fact, is to ask whether there is no negative power ring. Direct SPFA Judgment
#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<vector>#include<queue>#include<cmath>#include<cstring>using namespacestd;#defineINF 0XFFFFFFF#defineMAXN 520structedge{intE, W; Edge (intE=0,intw=0): E (E), W (w) {}};BOOLVIS[MAXN];intUSE[MAXN], DIST[MAXN];intN, M, W;vector<Edge>G[MAXN];voidInit () {memset (Vis,false,sizeof(VIS)); for(intI=0; i<=n; i++) Dist[i]= INF, Use[i] =0;}BOOLSPFA (intStar) {Edge P, Pn; Queue<Edge>Q; P.E= Star, P.W =0; Q.push (P); Use[star]=1; Dist[star]=0; while( !Q.empty ()) {P=Q.front (); Q.pop (); VIS[P.E]=false; intLen =g[p.e].size (); for(intI=0; i<len; i++) {Pn=G[p.e][i]; if(DIST[PN.E] > DIST[P.E] +PN.W) {DIST[PN.E]= DIST[P.E] +PN.W; if( !VIS[PN.E]) {VIS[PN.E]=true; Q.push (Pn); USE[PN.E]++; if(USE[PN.E] >= n && dist[pn.e] <0) return true; } } } } return false;}bool Slove () {for (int i=1; i<=n; i++) {Init (); if (SPFA (i)) return true; } return false;}intMain () {intT; CIN>>T; while(t--) {cin>> n >> M >>W; intA, B, C; for(intI=0; i<=n; i++) g[i].clear (); for(intI=0; i<m; i++) {scanf ("%d%d%d",&a,&b,&c); G[a].push_back (Edge (b,c)); G[b].push_back (Edge (a,c)); } for(intI=0; i<w; i++) {scanf ("%d%d%d",&a,&b,&c); G[a].push_back (Edge (b,-c)); } if(SPFA (i)) cout<<"YES"<<Endl; Elsecout<<"NO"<<Endl; } return 0;}
POJ wormholes 3259