题目链接在这里
Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 75644 Accepted: 28157
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.
Source
USACO 2006 December Gold
题目大意:就是农夫是否可以通过虫洞回到自己进入前的时间之前去。
题目思路:把农夫通过虫洞的时间回溯当作负边来进行处理,如果能回到去之前的状态的化,着用SPFA就存在着无线可松弛的情况,这时候只要用SFPA来判定是否由负环就可以解决问题。(这里我判断负环是判定一个数的入队列的次数是否大于等于n次)
同时我还在百度百科上看到,如果是正权图则使用DIJ,有负权则使用SPFA
下面就是代码了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define bit(x) x&(-x)
using namespace std;
const int maxn =5200;
const int inf=0x3f3f3f3f;
typedef long long ll;
int n,m,w,t;
int head[maxn],v[maxn],d[maxn];
int ans[maxn];
int cnt=0;
struct node
{
int y,t,p;
}s[maxn];
void add(int a,int b,int c)
{
s[++cnt]={b,c,head[a]};
head[a]=cnt;
}
int cmp(node a,node b)
{
return a.p<b.p;
}
int spfa(int x)
{
memset(v,0,sizeof(v));
memset(d,inf,sizeof(d));
d[x]=0;
queue<int>q;
q.push(x);
ans[x]++;
v[x]=1;
while(!q.empty()){
int w=q.front();
q.pop();
v[w]=0;
for(int i=head[w];~i;i=s[i].p){
int a=s[i].y;
int b=s[i].t;
if(d[a]>b+d[w]){
d[a]=b+d[w];
if(!v[a]){
q.push(a);
ans[a]++;
if(ans[a]>=n){
return 0;
}
v[a]=1;
}
}
}
}
return 1;
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&n,&m,&w);
memset(head,-1,sizeof(head));
memset(ans,0,sizeof(ans));
cnt=0;
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(int i=1;i<=w;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
add(a,b,-c);
}
int res=spfa(n);
if(res){
printf("NO\n");
}
else{
printf("YES\n");
}
}
return 0;
}