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 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
题目大意
判断 John 是否能实现从某一处出发,然后返回原地时间在出发时间之前,在道路中存在虫洞,穿过虫洞时间倒流且虫洞是单向的,而其余路径是双向的。
解题思路
边的权值即为经过此通路的时间,使用Bellman-Ford 算法对边进行松弛,只需判断是否存在负环。
注意输入:虫洞是负权,其余通路是正权。
代码实现
#include <iostream>
#include<cstdio>
using namespace std;
#define maxn 520
#define maxm 6006
const int INF=(1<<26);
struct node
{
int s,e,w;
}edge[maxm];
int dis[maxn],countt;
bool bellman_ford(int n)
{
int s,e,w;
for(int i=1;i<=n;i++)
dis[i]=INF;
dis[1]=0;
for(int i=0;i<n;i++)
for(int j=0;j<countt;j++)
{
s=edge[j].s;
e=edge[j].e;
w=edge[j].w;
if(dis[s]<INF&&dis[e]>dis[s]+w)
{
dis[e]=dis[s]+w;
if(i==n-1)
return true;
}
}
return false;
}
int main()
{
int T,n,m,l,s,e,w;
cin>>T;
while(T--)
{
countt=0;
cin>>n>>m>>l;
for(int i=0;i<m;i++)
{
cin>>s>>e>>w;
edge[countt].s=edge[countt+1].e=s;
edge[countt+1].s=edge[countt].e=e;
edge[countt++].w=w;
edge[countt++].w=w;
}
for(int i=0;i<l;i++)
{
cin>>s>>e>>w;
edge[countt].s=s;
edge[countt].e=e;
edge[countt++].w=-w;
}
if(bellman_ford(n)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
#include <iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
using namespace std;
#define maxn 2507*2+200
#define maxv 507
struct edge
{
int from;
int to;
int cost;
} ed[maxn];
int d[maxv],v,e;
bool find_negative_loop()
{
memset(d,0,sizeof(d));
for(int i=0; i<v; i++)
{
for(int j=0; j<e; j++)
{
edge es=ed[j];
if(d[es.to]>d[es.from]+es.cost)
{
d[es.to]=d[es.from]+es.cost;
if(i==v-1) return true;
}
}
}
return false;
}
int main()
{
int T;
int m,w;
int f,t,c;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&v,&m,&w);
e=0;
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&f,&t,&c);
ed[e].from=f;
ed[e].to=t;
ed[e].cost=c;
e++;
ed[e].from=t;
ed[e].to=f;
ed[e].cost=c;
e++;
}
for(int i=m; i<m+w; i++)
{
scanf("%d %d %d",&ed[e].from,&ed[e].to,&c);
ed[e].cost=-c;
e++;
}
if(find_negative_loop())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}