POJ3259 Wormholes —— spfa求负环

题目链接:http://poj.org/problem?id=3259

Wormholes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 55082   Accepted: 20543

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..NM (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, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) 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

题解:

代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <cmath>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
 13 #define ms(a,b) memset((a),(b),sizeof((a)))
 14 using namespace std;
 15 typedef long long LL;
 16 const double EPS = 1e-8;
 17 const int INF = 2e9;
 18 const LL LNF = 9e18;
 19 const int MOD = 1e9+7;
 20 const int MAXN = 1e3+10;
 21
 22 int N, M, W;
 23
 24 struct edge
 25 {
 26     int to, w, next;
 27 }edge[MAXN*MAXN];
 28 int cnt, head[MAXN];
 29
 30 void add(int u, int v, int w)
 31 {
 32     edge[cnt].to = v;
 33     edge[cnt].w = w;
 34     edge[cnt].next = head[u];
 35     head[u] = cnt++;
 36 }
 37
 38 void init()
 39 {
 40     cnt = 0;
 41     memset(head, -1, sizeof(head));
 42 }
 43
 44 int dis[MAXN], times[MAXN], inq[MAXN];
 45 bool spfa(int st)
 46 {
 47     memset(inq, 0, sizeof(inq));
 48     memset(times, 0, sizeof(times));
 49     for(int i = 1; i<=N; i++)
 50         dis[i] = INF;
 51
 52     queue<int>Q;
 53     Q.push(st);
 54     inq[st] = 1;
 55     dis[st] = 0;
 56     while(!Q.empty())
 57     {
 58         int u = Q.front();
 59         Q.pop(); inq[u] = 0;
 60         for(int i = head[u]; i!=-1; i = edge[i].next)
 61         {
 62             int v = edge[i].to;
 63             if(dis[v]>dis[u]+edge[i].w)
 64             {
 65                 dis[v] = dis[u]+edge[i].w;
 66                 if(!inq[v])
 67                 {
 68                     Q.push(v);
 69                     inq[v] = 1;
 70                     if(++times[v]>N) return true;
 71                 }
 72             }
 73         }
 74     }
 75     return false;
 76 }
 77
 78 int main()
 79 {
 80     int T;
 81     scanf("%d",&T);
 82     while(T--)
 83     {
 84         init();
 85         scanf("%d%d%d", &N, &M, &W);
 86         for(int i = 1; i<=M; i++)
 87         {
 88             int s, e, t;
 89             scanf("%d%d%d", &s, &e, &t);
 90             add(s, e, t);
 91             add(e, s, t);
 92         }
 93
 94         for(int i = 1; i<=W; i++)
 95         {
 96             int s, e, t;
 97             scanf("%d%d%d", &s, &e, &t);
 98             add(s, e, -t);
 99         }
100
101         int flag = 0;
102         for(int i = 1; i<=N; i++)
103         if(spfa(i))
104         {
105             flag = 1;
106             break;
107         }
108
109
110         if(flag)
111             puts("YES");
112         else
113             puts("NO");
114     }
115 }

时间: 2024-10-12 03:51:44

POJ3259 Wormholes —— 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

POJ3259 Wormholes(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! Eac

POJ 3259 Wormholes(SPFA判负环)

题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using na

「POJ3259」Wormholes - SPFA判负环

->戳我进原题 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 65211 Accepted: 24284 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-w

POJ-3259 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! Eac

[ An Ac a Day ^_^ ][kuangbin带你飞]专题四 最短路练习 POJ 2240 Arbitrage spfa求负环

题意就是问倒腾外币能不能升值 不用spfa 用其他的最短路算法也可以 松弛条件换成dist[v]<dist[u]*e[u][i].value 当然 貌似只有spfa有这个坑…… 有A  (value>1.0) A 这种情况……我的天 用Dij Floyd都只用判断如果松弛到了自己 那么一定有环 直接跳出就行 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<

POJ 3259 Wormholes【最短路/SPFA判断负环模板】

农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞.FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间.也许他就能遇到自己了:).为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农场的完整的映射到(1≤F≤5).所有的路径所花时间都

Poj3259--Wormholes(Spfa 判负环)

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

bzoj 1690: [Usaco2007 Dec]奶牛的旅行——分数规划+spfa判负环

Description 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有L(2 <= L <= 1000)座标志性建筑物(建筑物按1..L顺次编号),以及连接这些建筑物的P(2 <= P <= 5000)条道路. 按照计划,那天早上Farmer John会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到