POJ - 3259----Wormholes(Bellman-Ford)
来源:互联网 发布:传奇世界刷元宝软件 编辑:程序博客网 时间:2024/05/18 01:15
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 Input23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8Sample OutputNOYESHintFor 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.、
题目大意:有一个穿越时光的农夫,他有N个农场,每个农场有M条双向路,有W个单向虫洞,每条路有三个属性(s,e,t)分别代表的是从s到达e花费t时间,每个虫洞也有三个属性(s,e,t)分别代表的是s到达e花费-t时间,这个农夫想要满足自己的穿越时光的梦想,他想在他的农场里游荡一圈以后回到原点并且看到原来的自己,如果能满足他的梦想就输出“YES”,否者“NO”。
思路:Bellman-Ford算法(判断负权回路)
#include<algorithm>#include<iostream>#include<stdlib.h>#include<cstring>#include<cstdio>#include<cstdio>#include<cmath>#include<queue>using namespace std;const int MAX = 6000const int INF = 0x3f3f3f3f;//边集 struct edge{ int start,end,weigth;}E[MAX];int dis[MAX]; //出发点到其余点的最短距离 int edge,dot; //边集数,点集数 //单源最短路BF算法(判断负权回路) bool BF(int start){ for(int i=1 ; i<=dot ; i++){ dis[i] = (i==start)?0:INF; } //在对每条边进行1遍松弛的时候,生成了从s出发,层次至多为1的那些树枝。 //也就是说,找到了与s至多有1条边相联的那些顶点的最短路径; //对每条边进行第2遍松弛的时候,生成了第2层次的树枝, //就是说找到了经过2条边相连的那些顶点的最短路径……。 //因为最短路径最多只包含|v|-1 条边,所以,只需要循环|v|-1 次。 //循环次数为 起点到终点所经过的边数 最多为点数减一条边 for(int i=1 ; i<=dot-1 ; i++){ //枚举所有边集 bool flag = true; for(int j=1 ; j<=edge ; j++){ //这条边可以缩短路的长度,更新 if(dis[E[j].start]+E[j].weigth<dis[E[j].end]){ dis[E[j].end] = dis[E[j].start]+E[j].weigth; flag = false; } } //没有更新长度,说明已更新完 if(flag){ break; } } //判断负权回路 //再次加入一遍边集,若还能更新 说明存在负权回路 for(int j=1 ; j<=edge ; j++){ if(dis[E[j].start]+E[j].weigth<dis[E[j].end]){ return true; } } return false;}int main(void){ int T; cin>>T; while(T--){ //无向边,有向边 int num1,num2; cin>>dot>>num1>>num2; //输入权值(路径长度) edge = 1; for(int i=1 ; i<=num1 ; i++){ int x,y,w; cin>>x>>y>>w; E[edge].start = x; E[edge].end = y; E[edge++].weigth = w; E[edge].start = y; E[edge].end = x; E[edge++].weigth = w; } for(int i=1 ; i<=num2 ; i++){ int x,y,w; cin>>x>>y>>w; E[edge].start = x; E[edge].end = y; E[edge++].weigth = -w; } edge--; if(BF(1)){ cout<<"YES"<<endl; }else{ cout<<"NO"<<endl; } } return 0;}
- poj 3259 Wormholes(SPFA || Bellman-Ford)
- POJ 3259 Wormholes(Bellman-Ford)
- poj 3259 Wormholes (Bellman-ford)
- POJ 3259 Wormholes(Bellman-Ford判负环)
- poj 3259 Wormholes(Bellman-Ford)
- POJ 3259 Wormholes (SPFA&&BellMan Ford)
- POJ 3259 Wormholes (Bellman Ford判负环)
- POJ - 3259----Wormholes(Bellman-Ford)
- POJ 3259 Wormholes(bellman-ford)
- POJ 3259 Wormholes Bellman-Ford
- poj 3259 Wormholes(Bellman-Ford)
- POJ 3259 Wormholes (Bellman-ford)
- POJ 3259 Wormholes 【bellman-ford】
- poj 3259 Wormholes (Bellman-ford)
- 【POJ 3259 Wormholes】+ Bellman-Ford
- poj 3259 Wormholes(bellman-ford)
- POJ - 3259 Wormholes(判断负环, Bellman Ford,SPFA)
- POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)
- 1069. The Black Hole of Numbers
- java参数传递内存图
- swift设计模式学习 - 原型模式
- 随机点名
- [矩阵乘法+KMP] BZOJ1009: [HNOI2008]GT考试
- POJ - 3259----Wormholes(Bellman-Ford)
- 微机原理与接口技术学习笔记 -1
- 1070. Mooncake
- HDU2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活,多重背包+二进制优化
- 1196
- cookie讲解
- POJ 3458 Colour Sequence G++
- C++ Rvalue References Explained
- OpenSSL 生成证书