Poj 3259 Wormholes(spfa判负环)
Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 42366 Accepted: 15560
传送门
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
/有道翻译2.0/
虫洞
时间限制: 2000毫秒 内存限制: 65536 k
总提交: 42366 接受: 15560
描述
在探索他的许多农场,农场主约翰发现了一些惊人的虫洞。 虫洞非常特殊,因为它是一种单向路径送你到目的地之前进入虫洞! 每个FJ农场组成的 N (1≤ N ≤500)字段方便编号1 . . N , 米 (1≤ 米 ≤2500)路径,和 W (1≤ W ≤200)虫洞。
FJ穿越时光的狂热粉丝,他想做以下几点:开始在一些领域,通过一些路径和虫洞旅行,回到起始时间他最初的离开。 也许他将能够满足自己:)。
帮助FJ找出是否这是可能的,他将为你提供完整的地图 F (1≤ F ≤5)他的农场。 没有路径将超过10000秒,没有虫洞旅行可以带回FJ时间超过10000秒。
输入
1号线:一个整数, F 。 F 农场的描述。
每个农场的第1行:三个空格分隔的整数分别为: N , 米 , W
行2 . . 米 每个农场的+ 1:三个空格分隔的数字( 年代 , E , T )描述,分别为:之间的双向道路 年代 和 E 这需要 T 秒遍历。 两个字段可能被多个连接路径。
行 米 + 2 . . 米 + W 每个农场的+ 1:三个空格分隔的数字( 年代 , E , T )描述,分别为:一个路径的一种方式 年代 来 E 这也将旅行回来 T 秒。
输出
行1 . . F :对于每个农场,输出“YES”如果FJ能实现他的目标,否则输出“不”(不包括引号)。
样例输入
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
样例输出
NO
YES
提示
对于农场1,FJ不能穿越时间。
对于农场2,FJ可以穿越时间的周期1 - > 2 - > 3 - > 1,到达之前回到他的起始位置1秒。 他可以从任何地方开始循环来完成这项工作。
源
USACO 2006 12月黄金
/*
邻接矩阵+spfa判负环.
正权值建图双向.
负权值建图单向.
然后定理:若图中一点入队超过n次就出现了负环(bfs).
若路径中多次出现一点就出现了负环(dfs).
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define MAXN 25001
using namespace std;
int s[MAXN],head[MAXN],n,m,tot,cut,dis[MAXN],k;
bool b[MAXN];
struct data
{
int v;
int next;
int z;
}e[MAXN];
void add(int u,int v,int z)
{
e[cut].v=v;
e[cut].next=head[u];
e[cut].z=z;
head[u]=cut++;
}
bool spfa()
{
queue<int>q;
q.push(1);dis[1]=0;b[1]=true;s[1]++;
while(!q.empty())
{
int u=q.front();q.pop();b[u]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(dis[v]>dis[u]+e[i].z)
{
dis[v]=dis[u]+e[i].z;
if(!b[v])
{
b[v]=true;
s[v]++;
if(s[v]>=n) return 1;
q.push(v);
}
}
}
}
return 0;
}
void init()
{
cut=0;
memset(dis,0x7f,sizeof(dis));
memset(b,0,sizeof(b));
memset(head,-1,sizeof(head));
memset(s,0,sizeof(s));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
int x,y,z;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
for(int i=1;i<=k;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,-z);
}
if(spfa())
{
printf("YES\n");
}
else printf("NO\n");
}
return 0;
}