POJ 3259 虫洞旅行 spfa判负环

Wormholes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31425   Accepted: 11431

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

题目意思:john有n块农场(用1--n表示),农场之间有m条路径,每条路径有个权值为经过这条路径花费的时间,路径是双向的,农场中有w个虫洞(看过时间简史就知道了),虫洞表示为a,b,t即为从a农场到b农场花费 -t 时间(虫洞是单向的)。问john从某个农场出发再次回到该农场时时间是否是出发之前(有点绕嘴。。就是john从一个农场出发绕了一大圈再次回到农场时,此时的时间为出发之前即回到了过去)。

思路:判断负环即可。

代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 using namespace std;
 8
 9 #define N 505
10 #define inf 999999999
11
12 struct node{
13     int y, t;
14 };
15
16 int dis[N];
17 int visited[N];
18 int num[N];
19 vector<node>ve[N];
20 int n, m, w;
21
22 void init(){
23     int i;
24     for(i=0;i<=n;i++){
25         ve[i].clear();
26         dis[i]=inf;
27     }
28     memset(visited,0,sizeof(visited));
29     memset(num,0,sizeof(num));
30 }
31
32 int SPFA(int st){
33
34     int i, j, u;
35     node p, q;
36     queue<int>Q;
37     Q.push(st);
38     dis[st]=0;
39     visited[st]=1;
40     while(!Q.empty()){
41         u=Q.front();
42         Q.pop();
43         visited[u]=0;
44         for(i=0;i<ve[u].size();i++){
45             p=ve[u][i];
46             if(dis[p.y]>dis[u]+p.t){
47                 dis[p.y]=dis[u]+p.t;
48                 if(!visited[p.y]){
49                     visited[p.y]=1;
50                     Q.push(p.y);
51                 }
52                 num[p.y]++;
53                 if(num[p.y]>=n) return 1;
54             }
55         }
56     }
57     return 0;
58 }
59
60 main()
61 {
62     int i, j, k, t;
63     int x, y, z;
64     cin>>t;
65     while(t--){
66         scanf("%d %d %d",&n,&m,&w);
67         init();
68         node p;
69         for(i=0;i<m;i++){
70             scanf("%d %d %d",&x,&y,&z);
71             p.y=y;p.t=z;
72             ve[x].push_back(p);
73             p.y=x;;
74             ve[y].push_back(p);
75         }
76         for(i=0;i<w;i++){
77             scanf("%d %d %d",&x,&y,&z);
78             p.y=y;p.t=-z;
79             ve[x].push_back(p);
80         }
81         if(SPFA(1))
82             printf("YES\n");
83         else printf("NO\n");
84     }
85 }
时间: 2025-01-17 08:06:05

POJ 3259 虫洞旅行 spfa判负环的相关文章

poj 3259 Wormholes【spfa判断负环】

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

(简单) POJ 3259 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判断负环)

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

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

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

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