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
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 33 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
题意:
虫洞问题。现在有n个点,m条边,代表现在可以走的通路,比如从a到b和从b到a需要花费c时间,现在在地上出现了w个虫洞,虫洞的意义就是你从a到b话费的时间是-c(时间倒流,并且虫洞是单向的),现在问你从某个点开始走,能否回到从前。
分析:即检测负权回路,看是否有负权环,有负权环输出YES,否则输出NO;用BellmanFord()算法可以检测是否含有负权回路。
BellmanFord()算法核心语句:
for(k=1;k<=n-1;k++)
for(i=1;i<=m;i++)
if(dis[v[i]] > dis[u[i]]+w[i])
dis[v[i]] = dis[u[i]]+w[i];
//检测负权回路
int flag=0;
for(i=1;i<=m;i++)
if(dis[v[i]] > dis[u[i]]+w[i])
flag=1;
if(flag==1)
printf("此图含有负权回路\n");
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int cnt,m,n,w;
int inf = 9999999,dis[5005];
struct node
{
int u;
int v;
int t;
}e[5005];
void add(int u,int v,int t)
{
//因为Bellman-Ford算法要用u,v,t三者之间的关系,所以如果直接存储e[a][b] = c;不好操作
e[cnt].u = u;
e[cnt].v = v;
e[cnt].t = t;
cnt++;
}
int BellmanFord()
{
int i,j;
int flag=0;
for(i=1;i<=n;i++)
dis[i] = inf;
dis[1]=0;
//松弛操作
for(i=0;i<n;i++)
{
for(j=0;j<cnt;j++)
{
if(dis[e[j].v] > dis[e[j].u]+e[j].t)
dis[e[j].v] = dis[e[j].u] + e[j].t;
}
}
//检测负权回路
for(i=0;i<cnt;i++)
{
if(dis[e[i].v] > dis[e[i].u]+e[i].t)
return 1; //有负权边
}
return flag; //否则没有
}
int main()
{
int T,i;
int a,b,c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&w);
cnt=0;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c); //题中说明双向路径
add(b,a,c);
}
for(i=1;i<=w;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,-c); //题中已说明单向路径
}
int f = BellmanFord();
if(f==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}