POJ 3259 Wormholes 图论 贝尔曼-福特算法(Bellman-Ford)
来源:互联网 发布:access怎么读取数据库 编辑:程序博客网 时间:2024/05/24 01:48
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 toF (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 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 toE that also moves the traveler back T seconds.
Output
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 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.
#include<cstdio>#include<iostream>#include<cstring>using namespace std;struct Edge{ int u, v; int time;int flag;}edge[5500];int dist[600],n,m,w;void relax(int u, int v, int time){ if(dist[v] > dist[u] + time) dist[v] = dist[u] + time;}bool Bellman_Ford(){int i;for(i=1;i<=n;i++)dist[i]=100000; for(i=1; i<=n-1; ++i) for(int j=1; j<=2*m+w; ++j) relax(edge[j].u, edge[j].v, edge[j].time); bool flag = 1; for(i=1; i<=2*m+w; ++i) if(dist[edge[i].v] > dist[edge[i].u] + edge[i].time ) { flag = 0; break; } return flag;}int main(){int i,t;scanf("%d",&t);while(t--){memset(edge,0,sizeof(edge));scanf("%d%d%d",&n,&m,&w);for(i=1;i<=2*m;i+=2){scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].time);edge[i+1].u=edge[i].v;edge[i+1].v=edge[i].u;edge[i+1].time=edge[i].time;}for(i;i<=2*m+w;i++){scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].time);edge[i].time=-edge[i].time;}if(Bellman_Ford()==0)printf("YES\n");elseprintf("NO\n");}return 0;}
- POJ 3259 Wormholes 图论 贝尔曼-福特算法(Bellman-Ford)
- Bellman-Ford贝尔曼福特算法实现
- Bellman-ford(贝尔曼-福特算法)解析
- Bellman-Ford(贝尔曼-福特)算法求单源最短路径
- POJ 1860 Currency Exchange 贝尔曼-福特算法(Bellman-Ford)
- POJ 3259 Wormholes 贝尔曼福特算法判负环
- 算法/最短路径/Bellman-Ford贝尔曼福特算法
- URAL 1162 Currency Exchange (Bellman-Ford贝尔曼-福特算法)
- 算法学习 - Bellman-Ford(贝尔曼福特)算法(C++实现)
- 最短路径算法(2)—Bellman-Ford(贝尔曼-福特)算法
- 最短路 Bellman-Ford(贝尔曼-福特)
- Bellman-Ford(贝尔曼,福特)算法——解决负权边
- poj 3259 Wormholes(Bellman-ford 算法)
- POj 3259-Wormholes(Bellman-Ford算法)
- java实现图的最短路径(SP)的贝尔曼福特(Bellman-Ford)算法
- 最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)
- 最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)
- 最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)
- SIP 中的Dialog,call,session 和 transaction .
- poj 1666 Candy Sharing Game
- 数据结构——堆排序
- js获得浏览器地址栏的参数值
- b3log之pathVariableValueMap(final String requestURI)
- POJ 3259 Wormholes 图论 贝尔曼-福特算法(Bellman-Ford)
- 学习日志一----jsp模型一跟模型二的几个问题
- MySQL数据库增量备份及恢复方案
- 数据结构——二叉树的插入构造和删除操作
- 实时验证用户是否已经注册
- response 设置编码
- java_web学习第四天(request和response的学习)
- DWZ局部刷新
- 剑指offer--简易哈希表(13、36、57)