最短路(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.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
随机推荐
- Android计时器实例
布局文件 <Chronometer android:id="@+id/chronometer" android:layout_width="wrap_content ...
- linux /etc/rc.d/目录的详解
rc.d的内容如下: init.d/ :各种服务器和程序的二进制文件存放目录. rcx.d/: 各个启动级别的执行程序连接目录.里头的东西都是指向init.d/的一些软连接.具体的后边叙述. 还有三个 ...
- 【云计算】Docker Nginx示例
使用数据卷容器,配置Nginx Docker作为静态文件服务器 . 该方法是直接使用命令行,当然也可使用Dockerfile文件进行创建. 其实,使用docker创建nginx容器是很简单的,但要和数 ...
- 利用cocostudio库函数 实现左右滑动的背包栏UI (cocos2d-x 2.2.0)
.h #ifndef __COMMON_COMPONENTS__ #define __COMMON_COMPONENTS__ #include "cocos2d.h" #inclu ...
- 66. 有序数组构造二叉搜索树[array to binary search tree]
[本文链接] http://www.cnblogs.com/hellogiser/p/array-to-binary-search-tree.html [题目] 编写一个程序,把一个有序整数数组放到二 ...
- 【转】php json_encode中文为空的解决办法
转自:http://www.cnblogs.com/oldphper/p/4123415.html 本文列举3个方法,实现json_encode()后的string显示中文问题. 做接口时不需要,但存 ...
- iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析
做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...
- stm32——Flash读写
stm32——Flash读写 一.Flash简介 通过对stm32内部的flash的读写可以实现对stm32的编程操作. stm32的内置可编程Flash在许多场合具有十分重要的意义.如其支持ICP( ...
- 使用VS自带的报表RDLC结合报表控件ReportViewer使用
1.新建一个报表,设置报表之后,使用强类型的DataSet xsd 配置数据源,对报表中的使用最常用的是文本框和表格控件 2.新增WebForm窗体,拖一个ReportViewer控件,在WebFo ...
- 记32位Oracle客户端登录报12560协议适配器错误的解决办法
国庆买了一台新电脑ThinkPad E431,i5双核CPU,8G内存,硬盘比较坑爹5400转的500G,重点是预装win8的64位简体中文版.大学时买了第一台电脑神舟笔记本,因为神舟电脑便宜,所以没 ...