题目传送门

 /*
题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负)
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的更多相关文章

  1. Bellman_ford POJ 3259 Wormholes

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41728   Accepted: 15325 Descr ...

  2. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 3259 Wormholes

    SPFA求负环 模板题 记得每组处理之前clear vector /* *********************************************** Author :Sun Yuef ...

  3. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  4. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  5. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  6. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  7. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  8. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  9. POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)

    题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...

随机推荐

  1. 我们为之奋斗过的C#-----Bank系统

    首先感谢大家抽出宝贵的时间来看这个Bank系统,这是我最近学的Bank系统,你们看我刚一学完就给你们分享了我的所学以及学习的一些经验,所以大家一定要耐心看下去,真的你会有所收获的,不信你看看.下面话不 ...

  2. [Effective JavaScript 笔记] 第5条:避免对混合类型使用==运算符

    “1.0e0”=={valueOf:function(){return true;}} 是值是多少? 这两个完全不同的值使用==运算符是相等的.为什么呢?请看<[Effective JavaSc ...

  3. RHEL6.2下挂载光驱安装软件

    导读 在RHEL6.2命令行模式下挂载虚拟光驱安装软件也许会难倒许多新手,下面我给大家详细介绍一下.首先说明一下,本教程是以RHEL6.2版本下安装Apache为例.主要讲述挂载虚拟光驱的方法.环境: ...

  4. UNITY3D与iOS交互解决方案

    原地址:http://bbs.18183.com/thread-456979-1-1.html 本帖最后由 啊,将进酒 于 2014-2-27 11:17 编辑 “授人以鱼,不如授人以渔”,以UNIT ...

  5. IOS 入门开发之创建标题栏UINavigationBar的使用(二)

    IOS 入门开发之创建标题栏UINavigationBar的使用 http://xys289187120.blog.51cto.com/3361352/685746     IOS 开发有关界面的东西 ...

  6. Pascal’s Triangle

    vector<vector<int>> generate(int num) { vector<vector<int>> result; vector&l ...

  7. LVS-三种负载均衡方式比较

    1.什么是LVS? 首 先简单介绍一下LVS (Linux Virtual Server)到底是什么东西,其实它是一种集群(Cluster)技术,采用IP负载均衡技术和 基于内容请求分发技术.调度器具 ...

  8. VMware Snapshot 工作原理

    VMware中的快照是对VMDK在某个时间点的“拷贝”,这个“拷贝”并不是对VMDK文件的复制,而是保持磁盘文件和系统内存在该时间点的状态,以便在出现故障后虚拟机能够恢复到该时间点.如果对某个虚拟机创 ...

  9. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  10. Java for LeetCode 061 Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...