POJ3259——Wormholes(Bellman-Ford+SPFA)
Wormholes
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: 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 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
题目大意:
FJ童鞋家的农田里面有若干个虫洞,有一些虫洞是相连的,可以消耗一些时间爬过去(无向)。还有一些虫洞比较特殊,从一头爬到那一头之后时间会退后一些(有向)
FJ希望通过爬虫洞(?)来时时间退后,从而能看到另外一个自己。根据输入的虫洞信息来判断是否可以实现FJ的愿望。
解题思路:
说白了就是判断有向图有没有负环。Bellman-Ford算法和SPFA算法都可以用来判断是否有负环。
1.Bellman-Ford算法:
For i= to |G.V|-
For each edge(u,v)属于G.E
RELAX(u,v,w)
For each edge(u,v)属于G.E //此循环用来判断负环
If (v.d>u.d+w(u,v)
Return FALSE;
Return TRUE;
2.SPFA算法:
可以通过判断 顶点i进入队列的次数是否大于N-1来判断是否存在负环。
PS:相对于没有负环的有N个顶点的有向图来说,一个顶点最多松弛N-1次即可达到最短路。
Code(SPFA):
#include<stdio.h>
#include<string>
#include<iostream>
#include<limits.h>
#include<queue>
using namespace std;
int edge[][],times[],dis[];
bool vis[];
int N;
void init(int N)
{
for (int i=; i<=N; i++)
for (int j=; j<=N; j++)
edge[i][j]=INT_MAX;
}
bool SPFA(int begin)
{
for (int i=; i<=N; i++)
{
dis[i]=INT_MAX;
vis[i]=;
times[i]=;
}
queue<int> Q;
Q.push(begin);
dis[begin]=;
vis[begin]=;
times[begin]++;
while (!Q.empty())
{
begin=Q.front();
Q.pop();
vis[begin]=;
for (int j=; j<=N; j++)
if (j!=begin&&edge[begin][j]!=INT_MAX&&dis[j]>dis[begin]+edge[begin][j])
{
dis[j]=edge[begin][j]+dis[begin];
if (!vis[j])
{
Q.push(j);
vis[j]=;
times[j]++;
if (times[j]>=N) return ;
}
}
}
return ;
}
int main()
{
int T;
cin>>T;
while (T--)
{
int M,W;
cin>>N>>M>>W;
init(N);
for (int i=; i<=M; i++)
{
int x1,x2,x3;
scanf("%d%d%d",&x1,&x2,&x3);
if (x3<edge[x1][x2])
edge[x1][x2]=edge[x2][x1]=x3;
}
for (int i=; i<=W; i++)
{
int x1,x2,x3;
scanf("%d%d%d",&x1,&x2,&x3);
edge[x1][x2]=-x3;
}
bool ok=SPFA();
if (ok) printf("NO\n");
else printf("YES\n");
}
return ;
}
POJ3259——Wormholes(Bellman-Ford+SPFA)的更多相关文章
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】
题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...
- ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)
两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...
- poj1860 bellman—ford队列优化 Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22123 Accepted: 799 ...
- 蓝桥杯 algo_5 最短路 (bellman,SPFA)
问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个 ...
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- POJ 3259 Wormholes Bellman题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- poj3259 bellman——ford Wormholes解绝负权问题
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35103 Accepted: 12805 Descr ...
随机推荐
- lex&yacc4
yacc: we cannt use the $$ value dirictly. we need get it irrotly;
- [转]重叠IO
本章回答了如下几个问题: ◆ 什么是Overlapped I/O?为什么需要Overlapped I/O?如何让数据传输支持Overlapped I/O? ◆ 数据传输结束后,Win32提供了哪些方式 ...
- MFC中获取指针的方法
1.获取应用程序指针 CMyApp* pApp=(CMyApp*)AfxGetApp(); 2.获取主框架指针 CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针 CMainFr ...
- HTML5跨文档消息传递
HTML5定义了一些javascript API,其中有一个就是跨文档消息传递(cross-document-messaging简称XDM). 现在XDM已经作为一个规范独立了出来,名字为:Web M ...
- js字符串长度计算(一个汉字==两个字符)和字符串截取
js字符串长度计算(一个汉字==两个字符)和字符串截取 String.prototype.realLength = function() { return this.replace(/[^\x00-\ ...
- [SQL Server]树形结构的创建
对于SQL Server来说,构建显示一个树形结构不是一件容易的事情,逻辑构造能力不是它的强项.不过也不是说它没有能力干这个事情,只要换一种思维方式就可以理解它的工作原理. 例如,现在有一张表的内容如 ...
- MS也遵守规范了
CSS学的好不好,就看你对浏览器的兼容性处理的好不好. 拿opacity来说,本来写成opacity:0.3就完事了,但MS不来这套,它用filter,我们就不得不专门为它而 加上这么一大串(file ...
- 1056. Mice and Rice (25)
时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...
- matlab之点运算基本思想及几何平移变换
1.对数变换可以增强图像中较暗部分的细节,因为对数可以将较小的值放大,而较大的值缩小 2.伽马变换:y = (x + esp) ^ γ,x,y的取值范围是0到1,esp是补偿系数,γ为伽马系数.γ的不 ...
- PHP微信开发代码
1,SAE上申请服务器 2,绑定测试账号 3,Token验证 <?php /* http://www.cnblogs.com/xrhou12326/ CopyRight 2014 All Rig ...