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
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2.. M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2.. M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1.. F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.、
题目大意:有一个穿越时光的农夫,他有N个农场,每个农场有M条双向路,有W个单向虫洞,每条路有三个属性(s,e,t)分别代表的是从s到达e花费t时间,每个虫洞也有三个属性(s,e,t)分别代表的是s到达e花费-t时间,这个农夫想要满足自己的穿越时光的梦想,他想在他的农场里游荡一圈以后回到原点并且看到原来的自己,如果能满足他的梦想就输出“YES”,否者“NO”。
思路:Bellman-Ford算法(判断负权回路)
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int MAX = 6000
const int INF = 0x3f3f3f3f;
//边集
struct edge{
int start,end,weigth;
}E[MAX];
int dis[MAX]; //出发点到其余点的最短距离
int edge,dot; //边集数,点集数
//单源最短路BF算法(判断负权回路)
bool BF(int start){
for(int i=1 ; i<=dot ; i++){
dis[i] = (i==start)?0:INF;
}
//在对每条边进行1遍松弛的时候,生成了从s出发,层次至多为1的那些树枝。
//也就是说,找到了与s至多有1条边相联的那些顶点的最短路径;
//对每条边进行第2遍松弛的时候,生成了第2层次的树枝,
//就是说找到了经过2条边相连的那些顶点的最短路径……。
//因为最短路径最多只包含|v|-1 条边,所以,只需要循环|v|-1 次。
//循环次数为 起点到终点所经过的边数 最多为点数减一条边
for(int i=1 ; i<=dot-1 ; i++){
//枚举所有边集
bool flag = true;
for(int j=1 ; j<=edge ; j++){
//这条边可以缩短路的长度,更新
if(dis[E[j].start]+E[j].weigth<dis[E[j].end]){
dis[E[j].end] = dis[E[j].start]+E[j].weigth;
flag = false;
}
}
//没有更新长度,说明已更新完
if(flag){
break;
}
}
//判断负权回路
//再次加入一遍边集,若还能更新 说明存在负权回路
for(int j=1 ; j<=edge ; j++){
if(dis[E[j].start]+E[j].weigth<dis[E[j].end]){
return true;
}
}
return false;
}
int main(void){
int T;
cin>>T;
while(T--){
//无向边,有向边
int num1,num2;
cin>>dot>>num1>>num2;
//输入权值(路径长度)
edge = 1;
for(int i=1 ; i<=num1 ; i++){
int x,y,w;
cin>>x>>y>>w;
E[edge].start = x;
E[edge].end = y;
E[edge++].weigth = w;
E[edge].start = y;
E[edge].end = x;
E[edge++].weigth = w;
}
for(int i=1 ; i<=num2 ; i++){
int x,y,w;
cin>>x>>y>>w;
E[edge].start = x;
E[edge].end = y;
E[edge++].weigth = -w;
}
edge--;
if(BF(1)){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}