Wormholes
原题链接
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 57493 Accepted: 21455
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
虫洞
原题链接
时间限制:2000MS内存限制:65536K
总提交:57493接受:21455
描述
在探索他的许多农场时,约翰农夫发现了许多惊人的虫洞。一个虫洞非常奇特,因为它是一条单向的路径,可以在您进入虫洞之前将它送到目的地!每个FJ的农场包括N(1≤N≤500)方便地编号1..N,M(1≤边号≤2500)路径字段和W(1≤w≤200)虫洞。
由于FJ是一个狂热的时间旅行爱好者,他想要做到以下几点:从某个领域开始,穿过一些路径和虫洞,并在他最初离开之前的一段时间返回起跑场。也许他将能够见到自己:)。
为了帮助FJ发现这是否可行,他会为你提供完整的地图给他的农场的F(1≤F≤5)。没有路径需要超过10,000秒的时间才能移动,并且没有虫洞可以使FJ在时间上超过10,000秒。
输入
第1行:单个整数,F. F场说明如下。
每个农场的第1行:分别有三个空格分隔的整数:N,M和W.
每个农场的2..M + 1行:三个空间分隔的数字(S,E,T),分别描述:S和E之间需要T秒的双向路径。两个字段可能通过多条路径连接。
每个农场的行M + 2..M + W + 1:分别描述的三个以空格分开的数字(S,E,T):从S到E的单向路径也使旅行者返回T秒。
输出
第1..F行:对于每个农场,如果FJ可以实现其目标,则输出“YES”,否则输出“NO”(不包括引号)。
示例输入
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月号黄金
题解
题目的大意是给你若干条 正权双向边 和 负权单项边 来建图,问你是否有负权回路。
之所以可以这么转化,是因为我们需要绕一圈回到起点,那么必定有环。回到起点的路程还得小于 0,说明这个环上的边权加和为负数。
倘若一个点在负权回路上,我们的 spfa 刷最短路的时候(如果起点和这个点相互连通),会无数次便利到这个点,否则我们最多可以遍历到 n-1 次(刚开始的起点不算便利到)。由于实际上会遍历到无限次,为了保险,代码里我们把判断的条件提升到大于 n 次(大于 n 次肯定有事儿)。
所以,只要是一个没遍历过得点,我们就得以它为起点刷一趟最短路,同时记录每个点的遍历次数,一旦发现大于 n 次,说明肯定有负权回路。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=505,maxm=2505,maxw=205;
const int maxe=(maxm<<1)+maxw;
int n,m,k,lnk[maxn],nxt[maxe],son[maxe],w[maxe],q[maxn],his[maxn],tot;
LL dis[maxn];
bool vis[maxn];
int read()
{
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
void add(int x,int y,int s)
{
son[++tot]=y;nxt[tot]=lnk[x];lnk[x]=tot;w[tot]=s;
}
bool spfa(int x)
{
int til=1,hea=0;dis[x]=0;q[1]=x;
while (til!=hea)
{
int x=q[(++hea)%=maxn];vis[x]=0;
for (int i=lnk[x];i;i=nxt[i])
{
if (dis[son[i]]>dis[x]+w[i])
{
dis[son[i]]=dis[x]+w[i];
if (vis[son[i]]) continue;
vis[q[(++til)%=maxn]=son[i]]=1;
if (++his[son[i]]>n) return 1;
}
}
}
return 0;
}
int main()
{
int T=read();
while (T--)
{
n=read(),m=read(),k=read();tot=0;
memset(lnk,0,sizeof lnk);
for (int i=1;i<=m+k;i++)
{
int x=read(),y=read(),s=read();
if (i<=m) add(x,y,s),add(y,x,s);
else add(x,y,-s);
}
memset(his,0,sizeof his);
memset(vis,0,sizeof vis);
memset(dis,63,sizeof dis);
memset(q,0,sizeof q);
bool pd=0;
for (int i=1;i<=n&&!pd;i++)
if (!his[i]) (pd=spfa(i));
printf("%s\n",pd? "YES":"NO");
}
return 0;
}