Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
POJ 3259
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
Hint
For farm 1, FJ cannot travel back in time.
Submit
Status
Practice
POJ 3259
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
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条边是双向边代表从a到b所用的时间为c,其中w条边是虫洞为单向边并且走从a到b 的时间会倒退c问你最后会不会在从某块地出发后又回来,看到了离开之前的自己。
思路 :用spfa 判断在图中是否会出现负环如果会 就肯定可以看到原来的自己,所以以任意一点为起点出发寻找图中有没有出现负环 出现了就返回
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 22222222;
int n,m,w,cnt[600],vis[600],head[600],count1,d[600];
struct Edge
{
int u,v,w,next;
Edge(int a = 0, int b =0,int c=0)
{
u =a; v =b; w= c;
}
}e[10000];
void add(Edge q)
{
e[count1].u = q.u;
e[count1].v = q.v;
e[count1].w = q.w;
e[count1].next = head[q.u];
head[q.u] = count1++;
}
int bellman()
{
int i,j;
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
for(i = 1; i<= n;i++)
{
d[i] = maxn;
}
d[1] = 0;
queue<int> p;
p.push(1);
vis[1] = 1;
cnt[1]++;
while (!p.empty())
{
int s = p.front();
p.pop();
vis[s] = 0;
for(i = head[s];i!=-1;i = e[i].next)
{
int &begin = d[s];
int &end = d[e[i].v];
if(begin < maxn&& end > begin + e[i].w)
{
end = begin + e[i].w;
if(vis[e[i].v] == 0)
{
p.push(e[i].v);
vis[e[i].v] = 1;
if(++cnt[e[i].v] >n)
return 0;
}
}
}
}
return 1;
}
int main()
{
int a,b,c,i,j,t;
cin>>t;
while (t--)
{
memset(head,-1,sizeof(head));
count1 = 0;
scanf("%d %d %d",&n,&m,&w);
for(i = 0; i< m; i++)
{
scanf("%d %d %d",&a,&b,&c);
add(Edge(a,b,c));
add(Edge(b,a,c));
}
for(i =0; i< w; i++)
{
scanf("%d %d %d",&a,&b,&c);
add(Edge(a,b,-c));
}
if(bellman()==1)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}