POJ 3259 Wormholes Bellman题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/。未经本作者同意不得转载。 https://blog.csdn.net/kenden23/article/details/37737817
本题就是须要检查有没有负环存在于路径中,使用Bellman Ford算法能够检查是否有负环存在。
算法非常easy,就是在Bellman Ford后面添加一个循环推断就能够了。
题目故事非常奇怪,小心读题。
#include <stdio.h>
#include <string.h>
#include <limits.h>
const int MAX_N = 501;
const int MAX_M = 2501;
const int MAX_W = 201;
struct Edge
{
int src, des, wei;
//Edge(int s, int d, int w) : src(s), des(d), wei(w) {}
};
Edge edge[(MAX_M<<1)+MAX_W];
int dist[MAX_N];
int N, M, W, F;
bool cycleBellmanFord()
{
for (int i = 1; i <= N; i++) dist[i] = INT_MAX;
dist[1] = 0;
for (int i = 1; i < N; i++)
{
bool seperate = true;
for (int j = 0; j < (M<<1)+W; j++)
{
if (dist[edge[j].src] != INT_MAX &&
dist[edge[j].src]+edge[j].wei < dist[edge[j].des])
{
dist[edge[j].des] = dist[edge[j].src]+edge[j].wei;
seperate = false;
}
}
if (seperate) break;
}
for (int j = 0; j < (M<<1)+W; j++)
{
if ( dist[edge[j].src] != INT_MAX &&
dist[edge[j].src]+edge[j].wei < dist[edge[j].des]) return true;
}
return false;
}
int main()
{
scanf("%d", &F);
while (F--)
{
scanf("%d %d %d", &N, &M, &W);
int i = 0;
for ( ; i < (M<<1); i++)
{
scanf("%d %d %d", &edge[i].src, &edge[i].des, &edge[i].wei);
i++;
edge[i].des = edge[i-1].src;
edge[i].src = edge[i-1].des;
edge[i].wei = edge[i-1].wei;
}
for ( ; i < (M<<1)+W; i++)
{
scanf("%d %d %d", &edge[i].src, &edge[i].des, &edge[i].wei);
edge[i].wei = -edge[i].wei;
}
if (cycleBellmanFord()) puts("YES");
else puts("NO");
}
return 0;
}
POJ 3259 Wormholes Bellman题解的更多相关文章
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 最短路(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(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- poj 3259 Wormholes(最短路 Bellman)
题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...
随机推荐
- 条件选择case
SELECT COUNT(*),count(CASE b.AUTHORITY WHEN 'addAsmAccessControlList' THEN '1' ELSE NULL END) as aut ...
- vim的基本快捷操作(二)——可视模式
va{ 选中{}中间内容,包括{} va[ 选中[]中间内容,包括{} va( 选中()中间内容 ,包括{} vi< 选中<>中间内容,包括<> 将上面的a换成i,就不包 ...
- MySQL - Lock wait timeout exceeded
今天突然出了个奇怪的问题,原本正常启动的项目,在什么都没有修改的情况下,启动到一半的时候会卡住几分钟,几分钟后又成功启动了,刚好是卡在Quartz那里,还以为出什么奇奇怪怪的幺蛾子了,一看日志,数据库 ...
- 看过这些我明白了依赖注入及IoC
背景 最近一段时间在学习laravel框架,了解到这个框架一个比较核心的概念就是服务容器,而服务容器似乎又和依赖注入有关系.但是碍于官方关于这方面的讲解篇幅过少,所以自学了一下. 自学的途径也跟大家一 ...
- mysql添加外键语句
sql语句格式: · 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key (从表外键字段) references 主表(主 ...
- 总结下awk基本用法
命令格式: awk '{commands} [{other commands}]' awk 'condition{commands} [{other commands}]' 如:awk '$4==&q ...
- 通过list中值得名称查询索引号
>>> a = ['www','iplaypython','com']>>> a.index('iplaypython')
- [转载]Ubuntu Server下配置UTF-8中文环境
转载自:http://www.gaojinbo.com/ubuntu-server%E4%B8%8B%E9%85%8D%E7%BD%AEutf-8%E4%B8%AD%E6%96%87%E7%8E%AF ...
- final关键字和static关键字
final关键字:最终态--修饰成员变量,成员方法,类 final修饰变量: 基本类型变量:该变量为常量不能被赋值 引用类型变量:该地址不能被概变 地址不能被概变的原因: final Student ...
- robotframework+python3+selenium自动化测试环境搭建---第一集
1.安装python3.6 1.1 可选择Customize installation自定义安装内容,记得要勾选Add to PATH(这样就不用自己配置环境变量了). 1.2 安装成功后,可以输入p ...