poj Wormholes 【最短路径】【bellman_ford】

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

【题意】有f组数据,n个场所,m条路,w个虫洞,经过虫洞连接的路后可以时光倒流。求是否可以从任意点出发后回到原点

【思路】flody算法只能用于权值是正数的最短路径,这里的虫洞的权值为负,

AC代码:

 1 #include<cstdio>
 2 #define MAXM 2710
 3 #define MAXV 505
 4 #define inf 1<<29
 5
 6 struct node{
 7     int x,y,t;
 8 }edge[MAXM];
 9
10 int n,m,w;//n区域个数 m条路 w个虫洞
11
12 int bellman_ford()
13 {
14     int i,j,d[MAXV],flag=1,cnt=1;
15     for(i=1;i<=n;i++)
16         d[i]=inf;//出发点到i的距离全部初始化为无穷大
17     while(flag)
18     {
19         flag=0;//是为了把所有路走一遍
20         if(cnt++>n)
21             return 1;
22         //i是路的标号
23         for(i=1;i<=m;i++)//普通道路(非虫洞)找最短路
24         {
25             //分别找到edge[i].x和edge[i].y的最短距离
26             if(d[edge[i].x]+edge[i].t < d[edge[i].y])
27                 {d[edge[i].y] = d[edge[i].x]+edge[i].t;     flag=1;}
28             if(d[edge[i].y]+edge[i].t < d[edge[i].x])
29                 {d[edge[i].x] = d[edge[i].y]+edge[i].t;        flag=1;}
30         }
31         for(;i<=m+w;i++)//虫洞找最短路
32             if(d[edge[i].y] > d[edge[i].x]-edge[i].t)
33                 {d[edge[i].y] = d[edge[i].x]-edge[i].t;        flag=1;}
34     }
35     return 0;
36 }
37
38 int main()
39 {
40     int t,i;
41     scanf("%d",&t);
42     while(t--)
43     {
44         scanf("%d%d%d",&n,&m,&w);
45         for(i=1;i<=m+w;i++)
46             scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].t);
47         if(bellman_ford())
48             printf("YES\n");
49         else
50             printf("NO\n");
51     }
52     return 0;
53 }
时间: 2024-08-08 09:37:36

poj Wormholes 【最短路径】【bellman_ford】的相关文章

POJ Wormholes 最短路径 ballman_ ford 有负环

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #define MAX 9999999 7 8 using namespace std; 9 10 struct node 11 { 12 int u, v, w;//u 为起点,v为终点,w为u—>v的权值 13 }; 14 n

POJ 3259 Wormholes (bellman_ford算法判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32393   Accepted: 11771 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【Bellman_ford判断负环】

题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说明存在有负环 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector&

POJ Wormholes (SPFA)

http://poj.org/problem?id=3259 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 BEFOR

单源最短路径 Bellman_ford 和 dijkstra

首先两个算法都是常用于 求单源最短路径 关键部分就在于松弛操作 实际上就是dp的感觉 if (dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; ... } bellman_ford O(E*V) 但是配合队列可以 有spfa 可以达到O(kE) http://www.360doc.com/content/13/1208/22/14357424_335569176.shtml 并且bellman_ford还适用于负边 并

POJ 2240 Arbitrage Bellman_ford 判读是否存在正环

和POJ1860差不多,就是用bellmanford判读是否存在正环,注意的是同种货币之间也可以交换,就是说:A货币换A货币汇率是2的情况也是存在的. #include<stdio.h> #include<string.h> #include<cstring> #include<string> #include<math.h> #include<queue> #include<algorithm> #include<

POJ 3169 Layout bellman_ford 最短路

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstri

POJ Wormholes 3259

题目描述: Farmer John 在探索农场的时候 惊奇的发现一些虫洞,虫洞是一个特殊的东西,他是一个单向通道,他能到达虫洞的另一端, 可以穿越到达之前的时间.Farmer John 的由N个农场组成, 编号是 1-N, M条单向通道, W个虫洞. Farmer John 是个时空穿越迷, 他想要做到一下几点: 开始在一些农场,旅行通过虫洞或者是路,通过虫洞能返回到他之前的时间. 帮助Farmer John  他是否有可能穿越到之前的时间, 他将会给你完整的地图, 他有F个农场, 走过这段路径

poj 3020 最短路径覆盖 Antenna Placement

Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7329   Accepted: 3635 Description The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most st