【题目描述】
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.
在探索他的许多农场时,Farmer John发现了许多令人惊叹的虫洞。 虫洞是非常奇特的,因为它是一条单向路径,在您进入虫洞之前的某个时间将您带到目的地! FJ的每个农场包括N(1≤N≤500)个方便的编号为1 … N,M(1≤M≤2500)个路径,以及W(1≤W≤200)个虫洞。
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 : ) .
由于FJ是狂热的时间旅行爱好者,他想要做以下事情:从一些场地开始,穿过一些路径和虫洞,并在他最初离开前的一段时间返回起点。 也许他能够见到自己。
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.
为了帮助FJ找出这是否可能,他将为您提供他的农场F(1≤F≤5)的完整地图。 没有路径需要超过10,000秒的旅行时间,没有虫洞可以使FJ及时返回超过10,000秒。
【输入】
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.
第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秒。
【输出】
Lines 1… F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).
第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
【样例解释】
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.
对于1号农场,FJ无法及时返回。
对于农场2,FJ可以在1-> 2-> 3-> 1的周期内及时返回,在他离开前1秒返回到他的起始位置。他可以从周期的任何地方开始实现这一目标。
题目链接:https://cn.vjudge.net/problem/POJ-3259
代码如下:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=3000;
struct Edge{
int v,cost;
Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN+10];
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
}
bool vis[MAXN+10];
int dist[MAXN+10];
bool spfa(int n,int start)
{
for(int i=1;i<=n;i++)
{
vis[i]=false;
dist[i]=INF;
}
vis[start]=true;
dist[start]=0;
queue<int> Q;
while(!Q.empty())
Q.pop();
Q.push(start);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=false;
for(int i=0;i<E[u].size();i++)
{
int v=E[u][i].v;
int cost=E[u][i].cost;
if(dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
if(!vis[v])
{
vis[v]=true;
Q.push(v);
}
}
}
if(dist[start]<0)
return true;
}
return false;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
int F;
cin>>F;
while(F--)
{
int n,m,w;
cin>>n>>m>>w;
for(int i=1;i<=n;i++)
E[i].clear();
for(int i=1;i<=m;i++)
{
int s,e,t;
cin>>s>>e>>t;
addedge(s,e,t);
addedge(e,s,t);
}
for(int i=1;i<=w;i++)
{
int s,e,t;
cin>>s>>e>>t;
addedge(s,e,-t);
}
if(spfa(n,1))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}