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 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

RE: 农场分为几块, 有的地方存在虫洞, (有负权值(边权值为时间)的边)。 问能不能回到过去。  Spfa 判 负环。

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 const int INF = 0x3f3f3f3f;
 7 int dis[550], vis[550], used[550];   //used[]数组判负环;
 8 int n, m, t;
 9 struct Edge
10 {
11     int from, to, w, next;
12 } edge[5050];
13 int head[550], cnt;
14 void Add(int a, int b, int w)
15 {
16     Edge E = {a, b, w, head[a]};
17     edge[cnt] = E;
18     head[a] = cnt++;
19 }
20 bool Spfa(int src)
21 {
22      queue<int> q;
23     for(int i = 1; i <= n; i++)
24         dis[i] = INF;
25     dis[src] = 0; vis[src] = 1;
26     q.push(src);
27     while(!q.empty())
28     {
29     //    printf("1\n");
30         int u = q.front();
31         q.pop();
32         vis[u] = 0;
33         for(int i = head[u]; i != -1; i = edge[i].next)
34         {
35             int v = edge[i].to;
36             if(dis[v] > dis[u] + edge[i].w)
37             {
38                 dis[v] = dis[u] + edge[i].w;
39                 if(!vis[v])
40                 {
41                     vis[v] = 1;
42                     q.push(v);
43                     used[v]++;
44                 }
45             }
46             if(used[v] > n)     //存在负环;
47                 return true;
48         }
49     }
50     return false;
51 }
52 int main()
53 {
54     int nt;
55     scanf("%d", &nt);
56     while(nt--)
57     {
58         scanf("%d %d %d", &n, &m, &t);
59         int u, v, w; cnt = 0;
60         memset(vis, 0, sizeof(vis));
61         memset(used, 0 , sizeof(used));
62         memset(head, -1, sizeof(head));
63         for(int i = 0; i < m; i++)
64         {
65             scanf("%d %d %d", &u, &v, &w);
66             Add(u, v, w);
67             Add(v, u, w);
68         }
69         for(int i = 1; i <= t; i++)
70         {
71             scanf("%d %d %d", &u, &v, &w);
72             Add(u, v, -w);
73         }
74         if(Spfa(1))
75             printf("YES\n");
76         else
77             printf("NO\n");
78     }
79     return 0;
80 }
时间: 2024-08-24 00:28:42

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

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 ver

LightOj 1221 - Travel Company(spfa判负环)

1221 - Travel Company PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB A travel company is planning to launch their bus service in a new route. So they conducted a survey and made a list of all possible roads connecting diff

uva11090 Going in Cycle!! --- 二分+spfa判负环

给一个带权有向图,求其中是否存在环,若存在,输出环上边权的平均值最小的那个的平均值. 点的范围就50,感觉可以很暴力..但显然超时了 感觉方法好巧妙,二分平均值,将所有边权减去二分的那个值,然后spfa判断是否有负环 若有负环,则图中存在的所有环的边权平均值一定比枚举值大 反之则小,要是无论枚举值多大都没有负环,说明图中没有环. #include <iostream> #include <cstring> #include <string> #include <c

POJ-1860 Currency Exchange 【spfa判负环】

Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the

bzoj1690:[Usaco2007 Dec]奶牛的旅行(分数规划+spfa判负环)

前段时间准备省选没更,后段(?)时间省选考砸没心情更,最近终于开始恢复刷题了... 题目大意:有n个点m条有向边的图,边上有花费,点上有收益,点可以多次经过,但是收益不叠加,边也可以多次经过,但是费用叠加.求一个环使得收益和/花费和最大,输出这个比值. 显然这就是经典的分数规划题啊,就是最优比率环,那么就二分答案,将所有边(u,v)的边权改为[v的点权-(u,v)原边权*mid],这可以算是最优比率环的公式了吧,然后判一下是否有正环,有的话就说明答案可行.判正环有够别扭的,那就全部改成相反数然后