最短路(Bellman_Ford) POJ 3259 Wormholes
/*
题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负)
Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下去)
注意:双方向连通要把边起点终点互换后的边加上
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <queue>
using namespace std; const int MAXN = + ;
const int INF = 0x3f3f3f3f;
int d[MAXN];
struct NODE
{
int from, to, cost;
}node[MAXN]; bool Bellman_Ford(int s, int n, int m)
{
int x, y;
d[s] = ;
for (int k=; k<=n-; ++k)
{
for (int i=; i<=m; ++i)
{
NODE e = node[i];
d[e.to] = min (d[e.to], d[e.from] + e.cost);
}
} for (int i=; i<=m; ++i)
{
NODE e = node[i];
if (d[e.to] > d[e.from] + e.cost) return false;
} return true;
} void work(int n, int m)
{
bool flag = false;
flag = Bellman_Ford (, n, m); (!flag) ? puts ("YES") : puts ("NO");
} int main(void) //POJ 3259 Wormholes
{
//freopen ("B.in", "r", stdin); int N, M, W;
int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d%d", &N, &M, &W);
for (int i=; i<=N; ++i) d[i] = INF; int x, y, z;
for (int i=; i<=*M; ++i)
{
scanf ("%d%d%d", &x, &y, &z);
node[i].from = x; node[i].to = y; node[i].cost = z;
node[++i].from = y; node[i].to = x; node[i].cost = z;
} for (int i=*M+; i<=*M+W; ++i)
{
scanf ("%d%d%d", &x, &y, &z);
node[i].from = x; node[i].to = y; node[i].cost = -z;
} work (N, *M+W);
} return ;
}
最短路(Bellman_Ford) POJ 3259 Wormholes的更多相关文章
- Bellman_ford POJ 3259 Wormholes
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41728 Accepted: 15325 Descr ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 3259 Wormholes
SPFA求负环 模板题 记得每组处理之前clear vector /* *********************************************** Author :Sun Yuef ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
随机推荐
- xxxx is not translated in zh-rCN, zh-rTW
1.异常提示: "image_content" is not translated in zh-rCN, zh-rTW 2.错误原因: 根据报错提示,是说我没有对string文件做 ...
- Win10如何隐藏Windows Defender任务栏图标
导读 Windows 10 至发布以来就内置集成了 Windows Defender 安全防护应用,但有许多用户平常压根儿就没注意到它的存在.微软为了使安全防护功能更加明显,Windows 10 周年 ...
- 如何在Linux命令行中创建以及展示演示稿
导读 你在准备一场演讲的时候,脑海可能会先被图文并茂.形象华丽的演示图稿所占据.诚然,没有人会否认一份生动形象的演讲稿所带来的积极作用.然而,并非所有的演讲都需要TED Talk的质量.更多时候,演讲 ...
- 修改Capfile,在正式环境不再使用migration修改数据库
原因是这样的,运维不开放正式环境数据库的alter权限,所以每次都要给他们把sql语句发过去, 由他们来操作. https://github.com/capistrano/rails Require ...
- Matlab实现线性回归和逻辑回归: Linear Regression & Logistic Regression
原文:http://blog.csdn.net/abcjennifer/article/details/7732417 本文为Maching Learning 栏目补充内容,为上几章中所提到单参数线性 ...
- PHP网页数据正则采集
$url ="https://********"; $contents = file_get_contents($url); //抓取页面数据 //如果出现中文乱码使用下面代码 / ...
- php远程抓取网站图片并保存
以前看到网上别人说写程序抓取网页图片的,感觉挺神奇,心想什么时候我自己也写一个抓取图片的方法! 刚好这两天没什么事,就参考了网上一个php抓取图片代码,重点借鉴了 匹配img标签和其src属性正则的写 ...
- Firefox上Web开发工具库一览
Firefox的目标之一就是尽可能地使web开发者的生活更简单高效,并通过提供工具和具有很强扩展性的浏览器使人们创造出神奇的东西.使web开发者使用Firefox的时候,浏览器可以提供大量开发工具和选 ...
- SQL常见笔试面试题
sql理论题 1.触发器的作用? 答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的.它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化.可以 ...
- ubuntu下sh文件使用
可把shell命令批处理写进filename.sh文件 然后执行 chmod +x filename.sh 就可以执行./filename.sh了