POJ3259——Wormholes(Bellman-Ford+SPFA)
2014-07-24 15:15
393 查看
Wormholes
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
题目大意:
FJ童鞋家的农田里面有若干个虫洞,有一些虫洞是相连的,可以消耗一些时间爬过去(无向)。还有一些虫洞比较特殊,从一头爬到那一头之后时间会退后一些(有向)
FJ希望通过爬虫洞(?)来时时间退后,从而能看到另外一个自己。根据输入的虫洞信息来判断是否可以实现FJ的愿望。
解题思路:
说白了就是判断有向图有没有负环。Bellman-Ford算法和SPFA算法都可以用来判断是否有负环。
1.Bellman-Ford算法:
2.SPFA算法:
可以通过判断 顶点i进入队列的次数是否大于N-1来判断是否存在负环。
PS:相对于没有负环的有N个顶点的有向图来说,一个顶点最多松弛N-1次即可达到最短路。
Code(SPFA):
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
题目大意:
FJ童鞋家的农田里面有若干个虫洞,有一些虫洞是相连的,可以消耗一些时间爬过去(无向)。还有一些虫洞比较特殊,从一头爬到那一头之后时间会退后一些(有向)
FJ希望通过爬虫洞(?)来时时间退后,从而能看到另外一个自己。根据输入的虫洞信息来判断是否可以实现FJ的愿望。
解题思路:
说白了就是判断有向图有没有负环。Bellman-Ford算法和SPFA算法都可以用来判断是否有负环。
1.Bellman-Ford算法:
For i=1 to |G.V|-1 For each edge(u,v)属于G.E RELAX(u,v,w) For each edge(u,v)属于G.E //此循环用来判断负环 If (v.d>u.d+w(u,v) Return FALSE; Return TRUE;
2.SPFA算法:
可以通过判断 顶点i进入队列的次数是否大于N-1来判断是否存在负环。
PS:相对于没有负环的有N个顶点的有向图来说,一个顶点最多松弛N-1次即可达到最短路。
Code(SPFA):
#include<stdio.h> #include<string> #include<iostream> #include<limits.h> #include<queue> using namespace std; int edge[600][600],times[5505],dis[5505]; bool vis[5505]; int N; void init(int N) { for (int i=1; i<=N; i++) for (int j=1; j<=N; j++) edge[i][j]=INT_MAX; } bool SPFA(int begin) { for (int i=1; i<=N; i++) { dis[i]=INT_MAX; vis[i]=0; times[i]=0; } queue<int> Q; Q.push(begin); dis[begin]=0; vis[begin]=1; times[begin]++; while (!Q.empty()) { begin=Q.front(); Q.pop(); vis[begin]=0; for (int j=1; j<=N; j++) if (j!=begin&&edge[begin][j]!=INT_MAX&&dis[j]>dis[begin]+edge[begin][j]) { dis[j]=edge[begin][j]+dis[begin]; if (!vis[j]) { Q.push(j); vis[j]=1; times[j]++; if (times[j]>=N) return 0; } } } return 1; } int main() { int T; cin>>T; while (T--) { int M,W; cin>>N>>M>>W; init(N); for (int i=1; i<=M; i++) { int x1,x2,x3; scanf("%d%d%d",&x1,&x2,&x3); if (x3<edge[x1][x2]) edge[x1][x2]=edge[x2][x1]=x3; } for (int i=1; i<=W; i++) { int x1,x2,x3; scanf("%d%d%d",&x1,&x2,&x3); edge[x1][x2]=0-x3; } bool ok=SPFA(1); if (ok) printf("NO\n"); else printf("YES\n"); } return 0; }
相关文章推荐
- POJ 3259 Wormholes (Bellman-Ford/SPFA 判断是否存在负权环)
- POJ3259 Wormholes 解题报告--bellman_ford
- POJ3259(Wormholes)(Bellman-Ford判断负权图 )
- POJ 3259 Wormholes (Bellman-Ford/SPFA 判断是否存在负权环)
- POJ3259 Wormholes(Bellman-ford 负环)
- poj3259( spfa 和 bellman_ford)
- POJ3259 Wormholes (Bellman-Ford最短路径算法)
- POJ 3259-Wormholes (Bellman-Ford&&SPFA) (模板题)
- POJ3259《Wormholes》方法:Bellman-ford
- POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)
- poj3259 Wormholes 图的负权回路判定,Bellman_Ford
- (POJ 3259)Wormholes 判断负环 bellman_ford 或者 spfa
- Wormholes( POJ 3259)(Bellman-Ford+SPFA)(判断是否有负权环)(最短路模板)
- POJ3259 Wormholes SPFA 或者 bellman_ford
- 【最短路】poj3259-Wormholes(Bellman-Ford 最短路)
- POJ 3259 Wormholes (判断负环,SPFA或Bellman-Ford都可)
- poj 3259 Wormholes(SPFA || Bellman-Ford)
- POJ 3259 Wormholes (Bellman-Ford/SPFA 判断是否存在负权环)
- POJ训练计划3259_Wormholes(Bellman-Ford)(SPFA)
- 暑假-最短路(Bellman-ford、spfa)-F - Wormholes