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),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
随机推荐
- The North American Invitational Programming Contest 2017 题目
NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K A mysterious circular arrangement of black st ...
- String 类 常用函数
构造方法摘要: String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String. String(char[] value) ...
- 10.13课堂Scrum站立会议
项目名称:C#实现的连连看游戏 小组名称:计信F4 开会时间 :2016年10月11日 20:20~20:40 组长:张政 成员:张金生,武志远,李泉 内容: 昨日已完成: 张政:构建基础逻辑,实现游 ...
- WindowsXP开机就打开数字小键盘的几种方法
很多人WindowsXP登陆界面输入密码时,都要使用数字键盘,可是很多时候下都会出现小键盘灯不亮情况,非要每次都按一 下才行,是不是很麻烦呢?下面就把全面的解决方法告诉大家. 用户名和密码时,不要输入 ...
- dotnet core sdk 2.1 在centos下的安装
1. 安装微软的仓库 rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm 2. 修改仓库 ...
- DBGridEH序号的自动生成
序号的自动生成1.定义变量 private maxno:integer; public bmodified:boolean;2.写函数 function max(c ...
- Java中LinkedList的fori和foreach效率比较
在<Java中ArrayList的fori和foreach效率比较>中对ArrayList的两种循环方式进行了比较,本次对LinkedList的两种遍历方式进行效率的比较. 1. list ...
- iOS 扩展类方法之category!
一.category介绍 category可以不修改源代码的基础上扩展新的方法,Category只能用于方法,不能用于成员变量. 二.category创建 Example:我们扩展NSString类新 ...
- Redis 基础:Redis 事件处理
Redis 事件处理 Redis服务器是一个事件驱动程序,服务器需要处理以下两类事件: 文件事件(file event):Redis服务器通过套接字与客户端(或其他Redis服务器)进行连接,而文件事 ...
- 【刷题】BZOJ 5008 方师傅的房子
Description 方师傅来到了一个二维平面.他站在原点上,觉得这里风景不错,就建了一个房子.这个房子是n个点的凸多边形 ,原点一定严格在凸多边形内部.有m个人也到了这个二维平面.现在你得到了m个 ...