题目:http://poj.org/problem?id=3259

题意:主要就是构造图, 然后判断,是否存在负图,可以回到原点

/*
2
3 3 1 //N, M, W 1 2 2
1 3 4
2 3 1 3 1 3 //虫洞 3 2 1 //N, M, W 1 2 3
2 3 4 3 1 8 */
#include <iostream>
#include <cstring>
using namespace std; const int maxn = ( + + ) * + ;
const int INF = + ;
int N, M, W; //(from, to) 权值为cost
struct Edge {
int from,
to, cost;
Edge(int f = , int t = , int c = ) :
from(f), to(t), cost(c) {}
}; //边
Edge es[maxn]; int d[maxn]; //最短距离
int V, E; //顶点数,E边数 bool find_negative_loop()
{
memset(d, , sizeof(d)); for (int i = ; i < V; i++)
{
for (int j = ; j < E; j++) {
Edge e = es[j];
if (d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost; //如果第n次仍然更新了,则存在负圈
if (i == V - ) return true;
}
}
}
return false;
} void solve()
{
int F;
int from, to, cost; scanf("%d", &F);
while (F--)
{
scanf("%d%d%d", &N, &M, &W); //顶点数,边数, 虫洞数
V = N; E = ; // E = M * 2 应该
for (int i = ; i < M; ++i)
{
cin >> from >> to >> cost;
--from; --to;
//无向图 -- 去
es[E].from = from; es[E].to = to;
es[E].cost = cost; ++E;
//回 -- 再来一次
es[E].from = to; es[E].to = from;
es[E].cost = cost; ++E;
} for (int i = ; i < W; i++)
{
cin >> from >> to >> cost;
--from; --to;
es[E].from = from;
es[E].to = to;
//虫洞 - 回路
es[E].cost = -cost;
++E;
}
if (find_negative_loop()) {
printf("YES\n");
} else {
printf("NO\n");
}
}
} int main()
{
solve(); return ; }

POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图的更多相关文章

  1. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  2. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  3. poj 3259 Wormholes(bellman-ford判断负环)

    题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判 ...

  4. POJ 3259 Wormholes Bellman题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  5. poj 3259 Wormholes【spfa判断负环】

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36729   Accepted: 13444 Descr ...

  6. (简单) POJ 3259 Wormholes,SPFA判断负环。

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  7. POJ 3259 Wormholes【Bellman_ford判断负环】

    题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...

  8. 【POJ】3259 Wormholes

    题目链接:http://poj.org/problem?id=3259 题意:n个农场,m条双向路径,w条单向路径(虫洞).单向虫洞路径是负值.农夫想知道自己能不能看到自己(X). 题解:其实刚开始没 ...

  9. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

随机推荐

  1. 使用java开发微信公众平台(1)

    目录 开发服务器 域名验证 获取access_token 自定义菜单 个人账号不能定义url访问服务器,使用测试号就不用认证添加url了,进入公众平台测试账号 开发服务器 域名验证 进入公众平台测试账 ...

  2. teamcity执行jmeter脚本使用Executable with parameters方式不能正确运行解决思路

    如下图是选择command Line:Executable with parameters设置启动jmeter.bat  命令如下 command Executable: D:\apache-jmet ...

  3. js中常见算法

    一.面试80%都要问的数组去重 数组去重的方式有多种,其实面试中主要是想靠对对象的理解.还记得我第一次去面试的时候,去重的时候用了2个for循环. //1循环一次 var arr1 = [1,2,3, ...

  4. sublime Text 插件收录

    插件 1.SublimeText3常用快捷键和优秀插件 2.常用的sublime text 3插件-1 主题 1.https://www.jianshu.com/p/1a1113213faf 2.ht ...

  5. node 加密音频文件 和 解密音频文件

    fs.readFile('./downsuccess/'+name+'', {flag: 'r+', encoding: ''}, function (err, data) {           c ...

  6. C# Socket模拟发送接收

    Socket简介 通过TCP/IP与仪器或设备通讯,在C#语言中,我们通常采用Socket.本项目是一个简单的Socket建立服务监听与Socket作为客户端请求的一个示例. 项目结构 客户端项目 S ...

  7. MT【161】韦恩图

    (清华2017.4.29标准学术能力测试25) 若$N$的三个子集$A,B,C$满足$|A\cap B|=|B\cap C|=|C\cap A|=1$,且$A\cap B\cap C=\varnoth ...

  8. BZOJ 3224 普通平衡树 | 平衡树模板

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  9. 字典树(trie树)的指针简单实现pascal

    问题1:给你一个单词集合,支持添加,删除,询问某个单词出现次数. ; maxn=; type dictree=^rec; rec=record next:array[..maxword]of dict ...

  10. oracle的学习笔记

    Oracle的介绍 1. Oracle的创始人----拉里•埃里森 2. oracle的安装 [连接Oracle步骤](](https://img2018.cnblogs.com/blog/12245 ...