POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图
题目: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 判断是否存在负图的更多相关文章
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 3259 Wormholes(bellman-ford判断负环)
题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判 ...
- POJ 3259 Wormholes Bellman题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- poj 3259 Wormholes【spfa判断负环】
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36729 Accepted: 13444 Descr ...
- (简单) POJ 3259 Wormholes,SPFA判断负环。
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes【Bellman_ford判断负环】
题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...
- 【POJ】3259 Wormholes
题目链接:http://poj.org/problem?id=3259 题意:n个农场,m条双向路径,w条单向路径(虫洞).单向虫洞路径是负值.农夫想知道自己能不能看到自己(X). 题解:其实刚开始没 ...
- poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
随机推荐
- Linux操作系统(二)
SSD工作原理:http://www.360doc.com/content/15/0318/15/16824943_456186965.shtml HHD工作原理:http://blog.csdn.n ...
- sql中exists和not exists的用法
该文转载自:http://www.cnblogs.com/mytechblog/articles/2105785.html sql中exists,not exists的用法 exists : 强调的是 ...
- cxGrid 单元格回车移到下一行,当移到最后一个单元格时回车新增一行【转】
1 在TcxGridDBTableView中,设定属性 NewItemRow.Visible = True 2 在cxgrid中输入数据怎样回车换行 在TcxGridDBTableView中 将属 ...
- teamcity和jmeter结合进行接口自动化测试
(1)从teamcity官网下载jmeter插件:https://teamcity.jetbrains.com/repository/download/TeamCityPluginsByJetBrai ...
- 微信小程序使用函数的三种方法
使用来自不同页面的函数 函数写在util.js页面 function formatTime(date) { var year = date.getFullYear() var month = date ...
- List,Set和Map详解及其区别和他们分别适用的场景
Java中的集合包括三大类,它们是Set(集).List(列表)和Map(映射),它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现类.Set的实现类主要有HashS ...
- Java多线程(五) —— 线程并发库之锁机制
参考文献: http://www.blogjava.net/xylz/archive/2010/07/08/325587.html 一.Lock与ReentrantLock 前面的章节主要谈谈原子操作 ...
- 【Linux笔记】在后台执行scp,实现服务器间无密码文件拷贝。
远程备份大容量时常会有这样的情形:从远程备份的文件很大,需要很长时间,想在退出ssh后程序依然能继续在后台下载,可以通过建立服务器间安全信息关系和nohup的方式解决. 有两台服务器:A服务器IP 1 ...
- google 浏览器插件安装
谷歌访问助手
- ios开发之 -- xib关联自定义view
在xib下使用自定义的view,因为很多时候,可能幸亏自顶一个view,然后在view里面填充控件,但是需要重写很多无用的 代码,而且很容易出错不说,还很好工作量,使用xib的话,分钟搞定一个view ...