poj 3259 Wormholes[ bellman_ford 判负环]
来源:互联网 发布:没有性格特点知乎 编辑:程序博客网 时间:2024/05/18 19:22
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.
思路:普通路双向,正值;虫洞单向,负值;bellman_ford 判负环;
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#define INF 0x3f3f3f3f#define mem(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long LL;const int N=1002;int dist[N];int gra[N][N],vis[N][N];bool bellman_ford(int n,int s){ for(int i=1;i<=n;i++) { dist[i]=INF; } dist[s]=0; for(int i=1;i<=n-1;i++) { int flag=false; for(int j=1;j<=n;j++){ for(int k=1;k<=n;k++){ if(vis[j][k]==1&&dist[k]>dist[j]+gra[j][k]) { dist[k]=dist[j]+gra[j][k]; flag=true; } } } if(!flag) return false; } for(int j=1;j<=n;j++){ for(int k=1;k<=n;k++){ if(vis[j][k]==1&&dist[k]>dist[j]+gra[j][k]) return true; } } return false;}int main(){ int T;scanf("%d",&T); while(T--) { int n,m,w; scanf("%d%d%d",&n,&m,&w); mem(vis,0); int s,e,t; for(int i=0;i<m;i++) { scanf("%d%d%d",&s,&e,&t); if(vis[s][e]) { if(gra[s][e]>t) gra[s][e]=gra[e][s]=t; } else{ gra[s][e]=gra[e][s]=t; vis[s][e]=vis[e][s]=1; } } for(int i=0;i<w;i++) { scanf("%d%d%d",&s,&e,&t); gra[s][e]=0-t; vis[s][e]=1; } int flag=0; for(int i=1;i<=1;i++) //只用判1,就行了,图是联通的,不然临接矩阵超时!! { if(bellman_ford(n,i)) { flag=1; break; } }// for(int i=1;i<=n;i++)// printf("%d___",dist[i]);// printf("%d\n",gra[3][1]); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
- 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算法判负环)
- Wormholes (poj 3259 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(最短路SPFA/Bellman_Ford)
- (POJ 3259)Wormholes 判断负环 bellman_ford 或者 spfa
- poj Wormholes最短路问题(bellman_ford)
- poj Wormholes(Bellman_ford寻找负权环)
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
- 程序员的十个层次 你属于哪一层
- web即时通信2--基于Spring websocket实现web聊天室
- android的样式(style)与主题(theme)
- Windows Sockets 错误码及出错原因
- /bin/sh^M: bad interpreter: No such file or directory
- poj 3259 Wormholes[ bellman_ford 判负环]
- Valgrind
- 基于JQuery easyUI combobox实现了一个类似google、百度输入框的提示
- 我的商铺项目总结
- 每天一点数据库之-----Day 6 数据分组与数据分页
- 字符串匹配算法总结 (分析及Java实现)
- 高斯模糊算法
- Delphi 导出、导入Excel的一个快速方法
- [LeetCode]Delete Digits