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有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...
随机推荐
- Vue实现无痕刷新
一.什么是无痕刷新 在不刷新浏览器的情况下,实现页面的刷新. 传统的刷新页面方式 window.location.reload()原生 js 提供的方法 this.$router.go(0)vue 路 ...
- Django 模型中的CRUD
一.通过 ORM 向 DB 中增加数据 1.Entry.objects.create(属性=值,属性=值) Entry:具体要操作的Model类 ex: Author.objects.create(n ...
- 几道JS代码手写面试题
几道JS代码手写面试题 (1) 高阶段函数实现AOP(面向切面编程) Function.prototype.before = function (beforefn) { let ...
- 可重入锁:ReentrantLock理解使用
(一)可重入性 可重入性描述这样的一个问题:一个线程在持有一个锁的时候,它内部能否再次(多次)申请该锁.如果一个线程已经获得了锁,其内部还可以多次申请该锁成功.那么我们就称该锁为可重入锁.通过以下伪代 ...
- tushare下载安装教程与版本更新步骤
使用前提 安装Python 安装pandas:pip install pandas 安装lxml:pip install lxml 下载安装 方式1:pip install tushare,如果安装网 ...
- python之使用多个界定符分割字符串
主要是正则的编写 mport re line = 'asdf fjdk; afed, fjek,asdf, foo' # \s 匹配任意空白符,正则意思:分隔符可以是逗号,分号或者是空格,并且后面紧跟 ...
- Nginx基础优化
Nginx基础优化 1.隐藏nginx header版本号 1.1查看版本号 [root@Nginx ~]# curl -I http://www.yunwei.cn HTTP/1.1 200 OK ...
- 论文学习——《Learning to Compose with Professional Photographs on the Web》 (ACM MM 2017)
总结 1.这篇论文的思路基于一个简单的假设:专业摄影师拍出来的图片一般具备比较好的构图,而如果从他们的图片中随机抠出一块,那抠出的图片大概率就毁了.也就是说,原图在构图方面的分数应该高于抠出来的图片. ...
- 解决Zookeeper报错:conf is not executed because it is not in the whitelist的解决办法
1.echo wchp | nc localhost 2181 ,通过路径列出服务器 watch 的详细信息,且它会输出一个与 session 相关的路径.但是出现下面的错误. [root@xg61 ...
- kindeditor加入方法
1.editor文件夹拷进来 2. editor里jsp子文件夹里找到几个jar拷贝到网站的web-app里的lib下 3. 网页里 head里加个这个 <link rel="sty ...