最短路(Bellman_Ford) POJ 3259 Wormholes
/*
题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负)
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的更多相关文章
- Bellman_ford POJ 3259 Wormholes
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41728 Accepted: 15325 Descr ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 3259 Wormholes
SPFA求负环 模板题 记得每组处理之前clear vector /* *********************************************** Author :Sun Yuef ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
随机推荐
- Spring+Quartz实现定时任务的配置方法
1.Scheduler的配置 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ...
- PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)
如下所示,PHP连接MySQL报错: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' ...
- [Effective JavaScript 笔记]第36条:只将实例状态存储在实例对象中
理解原型对象与其实例之间是一对多的关系,对于实现正确的对象行为很重要.常见的错误是不小心将每个实例的数据存储到了其原型中. 示例 一个实现了树型数据结构的类可能将子节点存储在数组中. 实例状态在原型中 ...
- Minimum Depth of Binary Tree
二叉树的最小深度 采用递归的方式求左右结点的高度,注意判断一个结点是否是叶子结点(左右子树都不存大). int minDepth(TreeNode *root) { return minDepth(r ...
- [官方说明] 为什么ES4要分成两阶段?
从ES4第一个版本发布到现在,已经有段时间了,绝大多数坛友都已经适应了ES4所带来的新封装模式,但仍有部分新人和坛友对ES4的两阶段模式带有不解或曲解.本帖将就ES4的两阶段意义做出解释说明,希望更多 ...
- Swap Nodes & Reverse Nodes in k-Group
Swap Nodes | Given a linked list, swap every two adjacent nodes and return its head. Example Given 1 ...
- spring中注解的通俗解释
我们在没有用注解写spring配置文件的时候,会在spring配置文件中定义Dao层的bean,这样我们在service层中,写setDao方法,就可以直接通过接口调用Dao层,用了注解写法后,在配置 ...
- Windows 8操作技巧之快捷键大全
Windows 8操作系统发布之后,因为其新颖的界面和对触屏友好的设计,使许多长期使用Windows系统的用户,也觉得一时难以适应,一些操作方式也不知道如何去实现.在Windows系统中,快捷键无疑是 ...
- Java for LeetCode 149 Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- codeforces 488A. Giga Tower 解题报告
题目链接:http://codeforces.com/problemset/problem/488/A 题目意思:给出一个数a,范围是[-10^9, 10^9],问它最少需要加的一个正整数 b 是多少 ...