这道题目的意思让我纠结啊,看了一下第一组数据,明明可以回到原点,而且时间为零,怎么会输出NO了?在网上找了找都说是判断是否有负环,可是为什么啊?
又看了看题目,关键的句子就是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 :) .,在某点出发,通过一些道路和虫洞,能够回到出发点,且时间比出发时间小。最后我的理解是如果有负环存在,那就没有最短时间,一直无限的减小,那他肯定能在任何地方出发,回到出发点后时间比出发时间小。
Wormholes
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 45 Accepted Submission(s) : 21
Problem 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
代码如下:
<span style="color:#000000;">#include<stdio.h>
#include<string.h>
#include<queue>
#define M 0x3f3f3f3f
using namespace std;
int num;
int n,m,w;
int vist[510];
int d[510];
int head[510];
int count[510];
struct stu
{
int from,to,w,next;
};
stu edge[6000];
void inin()
{
num=0;
memset(head,-1,sizeof(head));
}
void addedge(int a,int b,int c)
{
stu E={a,b,c,head[a]};
edge[num]=E;
head[a]=num++;
}
bool SPFA(int sx)
{
memset(vist,0,sizeof(vist));
memset(d,0x3f,sizeof(d));
memset(count,0,sizeof(count));
d[sx]=0;vist[sx]=1;count[sx]=1;
queue<int>q;
q.push(sx);
while(!q.empty())
{
sx=q.front();
q.pop();
vist[sx]=0;
for(int i=head[sx];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(d[v]>d[sx]+edge[i].w)
{
d[v]=d[sx]+edge[i].w;
if(!vist[v])
{
vist[v]=1;
count[v]++;
if(count[v]>=n)//说明存在负环
return true;
q.push(v);
}
}
}
}
return false;
}
int main()
{
int t;
int a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&w);
inin();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
for(int i=1;i<=w;i++)
{
scanf("%d%d%d",&a,&b,&c);
c=-1*c;
addedge(a,b,c);
}
if(SPFA(1))
printf("YES\n");
else printf("NO\n");
}
return 0;
}</span>
<span style="color:#000000;">#include<stdio.h>
#include<string.h>
#include<queue>
#define M 0x3f3f3f3f
using namespace std;
int num;
int n,m,w;
int vist[510];
int d[510];
int head[510];
int count[510];
struct stu
{
int from,to,w,next;
};
stu edge[6000];
void inin()
{
num=0;
memset(head,-1,sizeof(head));
}
void addedge(int a,int b,int c)
{
stu E={a,b,c,head[a]};
edge[num]=E;
head[a]=num++;
}
bool SPFA(int sx)
{
memset(vist,0,sizeof(vist));
memset(d,0x3f,sizeof(d));
memset(count,0,sizeof(count));
d[sx]=0;vist[sx]=1;count[sx]=1;
queue<int>q;
q.push(sx);
while(!q.empty())
{
sx=q.front();
q.pop();
vist[sx]=0;
for(int i=head[sx];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(d[v]>d[sx]+edge[i].w)
{
d[v]=d[sx]+edge[i].w;
if(!vist[v])
{
vist[v]=1;
count[v]++;
if(count[v]>=n)//说明存在负环
return true;
q.push(v);
}
}
}
}
return false;
}
int main()
{
int t;
int a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&w);
inin();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
for(int i=1;i<=w;i++)
{
scanf("%d%d%d",&a,&b,&c);
c=-1*c;
addedge(a,b,c);
}
if(SPFA(1))
printf("YES\n");
else printf("NO\n");
}
return 0;
}</span>