Wormholes(SPFA判负权环)
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.
翻译
在探索他的许多农场时,农夫约翰发现了许多令人惊奇的虫洞。虫洞是非常特殊的,因为它是一条单行道,在你进入虫洞之前把你送到目的地!FJ的每一个农场都包括N(1≤N≤500)个区域,方便编号为1…N、M(1≤M≤2500)条路径和W(1≤W≤200)个虫洞。
因为FJ是一个狂热的时间旅行迷,他想做以下的事情:从某个领域开始,穿过一些路径和虫洞,在他最初离开之前的一段时间回到起始领域。也许他能认识自己。
为了帮助FJ了解这是否可行,他将向您提供其农场F(1≤F≤5)的完整地图。任何路径的运行时间都不会超过10000秒,任何虫洞都不能使FJ及时返回10000秒以上。
输入
第1行:一个整数,F.F农场描述如下。
每个农场的第1行:三个空间分隔的整数:n、m和w
每个农场的第2…m+1行:三个空格分隔的数字(s,e,t),分别描述:s和e之间的双向路径,需要t秒才能穿过。两个字段可以通过多个路径连接。
每一个农场的M+2…M+W+1行:分别描述的三个空格分隔的数字(S、E、T):从S到E的单向路径,也将旅行者向后移动T秒。
输出
第1…f行:对于每个农场,如果FJ能够实现其目标,则输出“是”,否则输出“否”(不包括报价)。
注意
1.虫洞路径每行第三个数据录入值应该为负值。
2.另外正常路径是双向的
3.虫洞是单向的
思路
这题主要要理解当花费的时间为负时,说明能在起点看到原来的自己。由题意可抽象出判断给出的图中是否存在负权环,存在则输出“YES”,否则输出“NO”。输入的路的边权为正,虫洞的边权为负,而SPFA算法思想在求最短路时,只有负权环对其有影响,也就是说可利用SPFA求最短路的入队出队过程判断是否出现负环。
代码:
#include<stdio.h>
#define INF 0x3f3f3f3f
int dis[10000],bak[10000],u[10000],v[10000],t[10000],check,flag,n,m,w;
int main()
{
int f;
scanf("%d",&f);
while(f--)
{
scanf("%d%d%d",&n,&m,&w);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&u[i],&v[i],&t[i]);
for(int i=m+1;i<=m+w;i++)
{
scanf("%d%d%d",&u[i],&v[i],&t[i]); //虫洞时间是负的·且单向
t[i]=-t[i];
}
for(int i=1;i<=n;i++)
dis[i]=INF;
dis[1]=0;
for(int k=1;k<n;k++)
{
for(int i=1;i<=n;i++)bak[i]=dis[i]; //将dis数组备份,可省时,提前跳出循环
for(int i=1;i<=m;i++)
{
if(dis[v[i]]>dis[u[i]]+t[i]) //农场两个方向都要判断
dis[v[i]]=dis[u[i]]+t[i];
if(dis[u[i]]>dis[v[i]]+t[i])
dis[u[i]]=dis[v[i]]+t[i];
}
for(int i=m+1;i<=m+w;i++)
{
if(dis[v[i]]>dis[u[i]]+t[i]) //注意!!!!!!!!!!!!!从v[i]-u[i]是正的,方向别弄错了。。
dis[v[i]]=dis[u[i]]+t[i];
}
check=0;
for(int i=1;i<=n;i++)
if(bak[i]!=dis[i])
{
check=1;
break;
}
if(check==0)break;
}
flag=0;
for(int i=1;i<=m;i++) //虫洞不用判断,因为本身就是负的,判断m就好了.加上也行。
if(dis[v[i]]>dis[u[i]]+t[i])flag=1;
if(flag)
printf("YES\n");
else printf("NO\n");
}
return 0;
}