标签:acm bellman_ford
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29971 | Accepted: 10844 |
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 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
Output
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
Source
这题什么意思读了半天也没弄懂。。在POJ上做题头一大难关就是读题意。。。
农民有N块地,每块地看做一个节点,有m条普通的路(双向),连接着两个节点,从一端走到另一端需要w时间(权值),还有wh条特殊的单向路,也就是题意中的虫洞,虫洞也连接着两个节点,但虫洞是单向的,从起点走到终点需要w时间,但这个时间是负的,也就是题意中所说的时光倒流,比如 一个虫洞连接着s -> e,在s处假设时间为6,走虫洞时间为4,那么走过去时光倒流,走到e时时间就变为了2 (6 -4,也就是虫洞这条路的权值为 -4 ) ,好神奇。。。。问有没有这样一种情况,就是第二次走到某个节点的时间比第一次走到该节点所用的时间短(题中的Perhaps he will be able to meet himself,时光倒流的作用)。假设存在这种情况,那么以某节点为起点和终点一定存在着一个回路,这个回路的权值是负的,这样第二次所用的时间一定比第一次少。
bellman-ford求最短路径算法中的第三步就是判断一个图(有向图,或无向图)中是否存在负权回路。
bellman-ford算法参考:http://blog.csdn.net/niushuai666/article/details/6791765
代码:
#include <iostream> #include <iostream> using namespace std; const int inf=10010; const int maxn=6000; struct Edge//边结构体 { int s,e,w; }edge[maxn]; int dis[maxn];//到达各顶点的距离 int nodeNum,edgeNum;//节点个数,边个数 bool bellman_ford() { for(int i=0;i<=nodeNum;i++) dis[i]=inf;//初始化 bool ok;//判断是否发生了松弛 for(int i=1;i<=nodeNum-1;i++) { ok=0; for(int j=1;j<=edgeNum;j++) { if(dis[edge[j].s]+edge[j].w<dis[edge[j].e]) { dis[edge[j].e]=dis[edge[j].s]+edge[j].w; ok=1; } } if(!ok)//没有发生松弛,及时退出 break; } for(int i=1;i<=edgeNum;i++)//寻找负权回路 if(dis[edge[i].s]+edge[i].w<dis[edge[i].e]) return true;//存在负权回路 return false; } int main() { int t;cin>>t; int n,m,wh; while(t--) { cin>>n>>m>>wh;//n为节点,m为双向边,wh为单向边 nodeNum=n; edgeNum=m*2+wh; int cnt=1; int s,e,w;//起点,终点,权值 for(int i=1;i<=m;i++) { cin>>s>>e>>w; edge[cnt].s=s; edge[cnt].e=e; edge[cnt++].w=w; edge[cnt].s=e; edge[cnt].e=s; edge[cnt++].w=w; } for(int i=1;i<=wh;i++) { cin>>s>>e>>w; edge[cnt].s=s; edge[cnt].e=e; edge[cnt++].w=-w;//注意是负权 } if(bellman_ford())//存在负权回路 cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路),布布扣,bubuko.com
[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)
标签:acm bellman_ford
原文地址:http://blog.csdn.net/sr_19930829/article/details/37771549