POJ3259——Wormholes(Bellman-Ford+SPFA)

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算法:

1 For i=1 to |G.V|-1
2     For each edge(u,v)属于G.E
3         RELAX(u,v,w)
4 For each edge(u,v)属于G.E   //此循环用来判断负环
5     If (v.d>u.d+w(u,v)
6         Return FALSE;
7 Return TRUE;

2.SPFA算法:

  可以通过判断 顶点i进入队列的次数是否大于N-1来判断是否存在负环。

  PS:相对于没有负环的有N个顶点的有向图来说,一个顶点最多松弛N-1次即可达到最短路。

Code(SPFA):

 1 #include<stdio.h>
 2 #include<string>
 3 #include<iostream>
 4 #include<limits.h>
 5 #include<queue>
 6 using namespace std;
 7 int edge[600][600],times[5505],dis[5505];
 8 bool vis[5505];
 9 int N;
10 void init(int N)
11 {
12     for (int i=1; i<=N; i++)
13         for (int j=1; j<=N; j++)
14             edge[i][j]=INT_MAX;
15 }
16 bool SPFA(int begin)
17 {
18     for (int i=1; i<=N; i++)
19     {
20         dis[i]=INT_MAX;
21         vis[i]=0;
22         times[i]=0;
23     }
24     queue<int> Q;
25     Q.push(begin);
26     dis[begin]=0;
27     vis[begin]=1;
28     times[begin]++;
29     while (!Q.empty())
30     {
31         begin=Q.front();
32         Q.pop();
33         vis[begin]=0;
34         for (int j=1; j<=N; j++)
35             if (j!=begin&&edge[begin][j]!=INT_MAX&&dis[j]>dis[begin]+edge[begin][j])
36             {
37                 dis[j]=edge[begin][j]+dis[begin];
38                 if (!vis[j])
39                 {
40                     Q.push(j);
41                     vis[j]=1;
42                     times[j]++;
43                     if (times[j]>=N) return 0;
44                 }
45             }
46     }
47     return 1;
48 }
49 int main()
50 {
51     int T;
52     cin>>T;
53     while (T--)
54     {
55         int M,W;
56         cin>>N>>M>>W;
57         init(N);
58         for (int i=1; i<=M; i++)
59         {
60             int x1,x2,x3;
61             scanf("%d%d%d",&x1,&x2,&x3);
62             if (x3<edge[x1][x2])
63                 edge[x1][x2]=edge[x2][x1]=x3;
64         }
65         for (int i=1; i<=W; i++)
66         {
67             int x1,x2,x3;
68             scanf("%d%d%d",&x1,&x2,&x3);
69             edge[x1][x2]=0-x3;
70         }
71         bool ok=SPFA(1);
72         if (ok) printf("NO\n");
73         else printf("YES\n");
74     }
75     return 0;
76 }

POJ3259——Wormholes(Bellman-Ford+SPFA)

时间: 2024-08-29 08:58:40

POJ3259——Wormholes(Bellman-Ford+SPFA)的相关文章

poj3259 Wormholes --- spfa判负环

又写了个bellman模板一直RE求解啊... #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x

ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可以求得一点到任意一点经过一条边的最短路,遍历两次可以求得一点到任意一点经过两条边的最短路...如 此反复,当遍历m次所有边后,则可以求得一点到任意一点经过m条边后的最短路(有点类似离散数学中邻接矩阵的连通性判定) POJ1556-The Doors 初学就先看POJ2240吧 题意:求从(0,5)到

POJ3259 Wormholes 【Bellmanford判断是否存在负回路】

很简单的bellmanford题目,这里比较详细:http://blog.csdn.net/lyy289065406/article/details/6645790 直接代码 #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace s

POJ 3259 Wormholes Bellman题解

本题就是需要检查有没有负环存在于路径中,使用Bellman Ford算法可以检查是否有负环存在. 算法很简单,就是在Bellman Ford后面增加一个循环判断就可以了. 题目故事很奇怪,小心读题. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const int MAX_W = 201; struct E

POJ-1847 最短路裸题,Floyd, Bellman, Dijkstra, Spfa

POJ 1847 题意:n个点,电车从A到B.每个点可以到其它ki个点,但默认只通往给出的第一个点,如果要到其它点,必须改变轨道方向一次.问A到B最少改变几次轨道方向. 总结:裸裸的最短路,所以,狠狠的把Floyd, Bellman, Dijkstra, Spfa都给撸了一遍.一个字,爽! // POJ-1847 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdl

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22405   Accepted: 8095 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

Bellman - Ford 算法解决最短路径问题

Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力不从心了,而Bellman - Ford算法可以解决这种问题. Bellman - Ford 算法可以处理路径权值为负数时的单源最短路径问题.设想可以从图中找到一个环路且这个环路中所有路径的权值之和为负.那么通过这个环路,环路中任意两点的最短路径就可以无穷小下去.如果不处理这个负环路,程序就会永远运

Bellman—Ford算法思想

---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G运行Bellman—Ford算法的结果是一个布尔值,表明图中是否存在着一个从源点s可达的负权回路.若存在负权回路,单源点最短路径问题无解:若不存在这样的回路,算法将给出从源点s到图G的任意顶点v的最短路径值d[v] Bellman—Ford算法流程 分为三个阶段: (1)初始化:将除源点外的所有顶点

poj3259 bellman——ford Wormholes解绝负权问题

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 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 p