Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Output
Sample Input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
Hint
题目大意:
算法:flod算法
思路: 田地间的双向路径加边,权值为正
孔洞间的单向路径加边,权值为负【可以回到以前】
判断有向图是否存在负环
因为如果存在了负数环,时间就会不停的减少,
那么 FJ 就可以回到以前更远的地方,肯定能遇到以前的自己的
#include#include #include using namespace std;const int maxn = 510;const int inf = 9999999;struct Node{ int u,v; int t;}E[2500*2+200+10];int d[maxn];int c;int n,m,w;bool flod(){ for(int i=1;i d[u]+t){ //g跟新每个点时间 d[v]=d[u]+t; flag = true; } } if(!flag) return false; //如果不能跟新则不存在负边 } for(int i=0;i d[E[i].u]+E[i].t) return true; } return false;}int main(){ int f; scanf("%d",&f); while(f--){ scanf("%d%d%d",&n,&m,&w); c=0; for(int i=0;i