题目传送门

 /*
题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负)
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. hadoop学习之一

         Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.Hadoop的框架最核心的设计 ...

  2. 绕过 <?PHP exit('Access Denied'); ?> 限制

    绕过 <?PHP exit('Access Denied'); ?> 限制   <?php $shellcode='PD9waHBpbmZvKCk7Pz4';//   base64_ ...

  3. linux增加自定义path和manpath

    linux安装软件到自定义路径时,新安装的命令需要带上路径才可以执行,不能像系统自带命令那样可以直接使用. 这个时候可以通过修改环境变量PATH和MANPATH,来实现像系统命令一样使用新安装的命令并 ...

  4. django inclusion_tag

    一种比较普遍的tag类型是只是渲染其它模块显示下内容,这样的类型叫做Inclusion Tag. 例如,实现以下tag: {% books_for_author author %} 渲染结果为: &l ...

  5. 对 Linux 新手非常有用的 20 个命令

    参考:http://www.oschina.net/translate/useful-linux-commands-for-newbies 英文原文:http://www.tecmint.com/us ...

  6. c++0x新特性实例(比较常用的)

    //array #include <array> void Foo() { array<> a; generate(a.begin(),a.end(),rand); sort( ...

  7. iOS 利用self.navigationItem.backBarButtonItem修改后退按钮文字

    @property(nonatomic,retain) UIBarButtonItem *backBarButtonItem; // Bar button item to use for the ba ...

  8. 2.python基础深入(元组、字符串、列表、字典)

    一,对象与类 对象: python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,玩的手机就是对象. 我们通过描述属性(特征)和行为来描述一个对象的. 在python中,一个对象的特 ...

  9. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  10. Struts2中的ActionContext、OGNL及EL的使用

    文章分类:Java编程 本文基于struts2.1.8.1,xwork2.1.6 1.EL         EL(Expression Language)源于jsp页面标签jstl,后来被jsp2.0 ...