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 requiresT 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个虫洞,然后接下来M行的每行是三个数S,E,T代表:S和E之间的双向路径,需要T秒才能穿过,接下来W行,每行三个数S,E,Ts,表示从S到E的·单向路径,可让旅行者返回Ts秒虫洞使条单向路,会在你进入虫洞之前把你传送到目的地,就是当你过去的时候时间会倒退Ts。求会不会在最初离开之前的某个时间之前回到起点。
思路:这个题就是问我们是否有负权回路。在输遇到虫洞的三个数时把时间输成负数就行了,然后用SPFA算法。代码如下:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define maxx 100000
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int to;
int next;
int w;
} e[maxx];
int flag;
int dis[maxx];
int head[maxx],cn[maxx],sum[maxx],vis[maxx];
int cnt,m,n;
void add(int x,int y,int d)
{
e[cnt].to=y;
e[cnt].w=d;
e[cnt].next=head[x];
head[x]=cnt++;
}
void spfa()
{
queue<int>q;
q.push(1);
dis[1]=0;
vis[1]=1;
while(!q.empty())
{
int u=q.front();
vis[u]=0;
q.pop();
for(int i=head[u]; i!=-1; i=e[i].next)
{
int v=e[i].to;
if(dis[v]>dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
if(!vis[v])
{
sum[v]++;
vis[v]=1;
q.push(v);
if(sum[v]>=n)
{
flag=1;
break;
}
}
}
}
if(flag)
break;
}
}
int main()
{
int T,c;
scanf("%d",&T);
while(T--)
{
memset(dis,inf,sizeof(dis));
memset(head,-1,sizeof(head));
memset(cn,0,sizeof(cn));
memset(sum,0,sizeof(sum));
memset(vis,0,sizeof(vis));
cnt=0;
flag=0;
scanf("%d%d%d",&n,&m,&c);
int x,y,z;
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
for(int i=0; i<c; i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,-z);
}
spfa();
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
925

被折叠的 条评论
为什么被折叠?



