[ACM] POJ 3259 Wormholes (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
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
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
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.
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最短路径,推断是否存在负权回路)的更多相关文章
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes Bellman题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 【原创】POJ 3259 Wormholes(Bellman-Ford) && 简介Bellman-Ford算法
[原创] 题目大意 John有N个农场,一共有M条边,在农场上出现了W个虫洞(W是一条边),其中M是双向普通边,W是单向虫洞边.John穿行于农场之间每经过一条边(S到E)的时间为+T,每经过虫洞会时 ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- poj 3259 bellman最短路推断有无负权回路
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36717 Accepted: 13438 Descr ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
随机推荐
- Servlet的学习之Response响应对象(3)
本篇来说明响应对象HttpServletResponse对象的最后一点内容. 首先来看响应对象控制浏览器定时刷新,在我的web应用[myservlet]中创建Servlet,在该Servlet中设置响 ...
- 如何在一个jpg图片上面叠加文字
1.将jpg转为bmp格式 2.在bmp文件上写上所需文字 3.将写入文字的bmp文件重新转为jpg格式 http://dev.csdn.net/develop/article/22/22948.sh ...
- python与其它语言进行比較
近期python语言貌似比較火, 今天闲来无事,简单的看了下,算是个入门吧.一门语言之所以值得这么多人去学,必然有它的独到之处,以下我们就用python和其它语言做个比較. Pythond VS C# ...
- HDOJ 2442 -bricks 状态压缩DP 一直TLE.打表过的..
有5个砖块..加上一个空着不放..那么有6种状态..所以很明显的可以用6进制的状态DP... 不过这么做..我觉得我已经能优化的都优化了...还是超时..一看数据范围是100*6..打表先AC了.. ...
- C++编程规范之11:隐藏信息
摘要: 不要泄密,不要公开提供抽象的实体的内部信息. 为了尽量减少操作抽象的调用代码和抽象的实现之间的依赖性,必须隐藏实现内部的数据.否则,调用代码就能够访问该信息,或者更糟,操作该信息,而原来应属于 ...
- ABP模块设计
ABP模块设计 返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术 ...
- MFC实现多风格真彩色大图标工具栏按钮
研究zlib库,想实现一个类似winrar功能的小东东,打开winrar界面看它的工具栏比较好看于是动手想做一个,当然资源也使用的是winrar附带的.下面是截图:真彩色(32位)32*32大图标工具 ...
- [读书笔记]黑客与画家[Hackers.and.Painters]
(书生注:这本书写的不错.针对程序员,可以带来不同角度的想法,有助于反思自己的程序员工作.我甚至从中发现了自己爱用铅笔的原因... 尤其是其中关于黑客的定义,包括黑客认为的乐趣和目的,让人更深层次思 ...
- LV在系统重启后不能自动激活(boot.lvm&after.loca)
同类相关文章:http://blog.csdn.net/laven54/article/details/9121661 最近发现suse11sp2的系统解决了异常死机的问题之后,又引入了另外的问题,比 ...
- Delphi回调函数及其使用
Delphi回调函数及其使用 1 回调函数的概述 回调函数是这样一种机制:调用者在初始化一个对象(这里的对象是泛指,包括OOP中的对象.全局函数等)时,将一些参数传递给对象,同时将一个调用者可以访问的 ...