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 toF (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.
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
NO YES
#if 0
#include<iostream>
#include<cstring>
using namespace std;
int n,m,no,k; //点,路,黑洞
struct lu
{
int a,b,t;
}mapp[10000];
int dis[10000];
void ford_one()
{
memset(dis,0x7f,sizeof(dis));
dis[1]=0;
int a,b,t;
for(int i=1; i<=n-1; i++)
{
bool flag=0;
for(int j=1; j<=k; j++)
{
a=mapp[j].a;
b=mapp[j].b;
t=mapp[j].t;
if(dis[b]>dis[a]+mapp[j].t)
{
dis[b]=dis[a]+mapp[j].t;
flag=1;
}
}
if(flag==0)
break;
}
}
int ford_two()
{
int a,b,t;
for(int j=1; j<=k; j++)
{
a=mapp[j].a; b=mapp[j].b;t=mapp[j].t;
if(dis[b]>dis[a]+mapp[j].t)
return 1;
}
return 0;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m>>no;
k=0;
for(int i=1; i<=m; i++)
{
int aa,bb,xx;
cin>>aa>>bb>>xx;
k++;
mapp[k].a=aa;
mapp[k].b=bb;
mapp[k].t=xx;
k+=1;
mapp[k].a=bb;
mapp[k].b=aa;
mapp[k].t=xx;
}
for(int i=1; i<=no; i++)
{
int aa,bb,xx;
cin>>aa>>bb>>xx;
k+=1;
mapp[k].a=aa;
mapp[k].b=bb;
mapp[k].t=-xx;
}
ford_one();
if(ford_two())
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
}
#endif