博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3259 Wormholes (最短路)
阅读量:6998 次
发布时间:2019-06-27

本文共 3885 字,大约阅读时间需要 12 分钟。

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 34302   Accepted: 12520

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

23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8

Sample Output

NOYES

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.
 
 
 
以每个点为起点跑一次,检测是否有负环。
1 #include 
2 #include
3 using namespace std; 4 5 const int INF = 0xfffffff; 6 const int SIZE = 5500; 7 int D[505]; 8 int N,M,W,F; 9 struct Node10 {11 int from,to,cost;12 }G[SIZE];13 14 bool Bellman_Ford(int);15 bool relax(int,int,int);16 int main(void)17 {18 scanf("%d",&F);19 while(F --)20 {21 scanf("%d%d%d",&N,&M,&W);22 int i = 0;23 while(i < 2 * M)24 {25 scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);26 i ++;27 G[i] = G[i - 1];28 swap(G[i].from,G[i].to);29 i ++;30 }31 while(i < W + M * 2)32 {33 scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);34 G[i].cost = -G[i].cost;35 i ++;36 }37 bool flag = true;38 for(int i = 1;i <= N;i ++)39 if(!Bellman_Ford(i))40 {41 puts("YES");42 flag = false;43 break;44 }45 if(flag)46 puts("NO");47 }48 49 return 0;50 }51 52 bool Bellman_Ford(int s)53 {54 fill(D,D + 505,INF);55 D[s] = 0;56 bool update;57 58 for(int i = 0;i < N - 1;i ++)59 {60 update = false;61 for(int i = 0;i < M * 2 + W;i ++)62 if(relax(G[i].from,G[i].to,G[i].cost))63 update = true;64 if(!update)65 break;66 }67 for(int i = 0;i < M * 2 + W;i ++)68 if(relax(G[i].from,G[i].to,G[i].cost))69 return false;70 return true;71 }72 73 bool relax(int from,int to,int cost)74 {75 if(D[to] > D[from] + cost)76 {77 D[to] = D[from] + cost;78 return true;79 }80 return false;81 }
Bellman_Ford

 

转载于:https://www.cnblogs.com/xz816111/p/4513056.html

你可能感兴趣的文章
文件操作的其他模式
查看>>
链表与顺序表的对比
查看>>
Angularjs总结(七) 路由及请求服务等
查看>>
Bindservice开启服务特点
查看>>
centos session
查看>>
Google Code Jam 2014 资格赛:Problem D. Deceitful War
查看>>
上传文件
查看>>
noip rp++
查看>>
js中数组的合并和对象的合并
查看>>
解决 UE4 无法找到。generated.h 办法
查看>>
python 读取SQLServer数据插入到MongoDB数据库中
查看>>
TCP的三次握手与四次挥手(详解+动图)
查看>>
Centos 6.5 磁盘修复 破解删除root密码
查看>>
某游戏浏览器Flash加速dll调用,打造我们自己的Flash加速器
查看>>
XML序列化与反序列化
查看>>
Redis数据操作命令
查看>>
java 注解
查看>>
DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
查看>>
2016"百度之星" - 资格赛(Astar Round1)
查看>>
批量修改横断面图高程范围
查看>>