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

本题就是须要检查有没有负环存在于路径中,使用Bellman Ford算法能够检查是否有负环存在。

算法非常easy,就是在Bellman Ford后面添加一个循环推断就能够了。

题目故事非常奇怪,小心读题。

#include <stdio.h>
#include <string.h>
#include <limits.h> const int MAX_N = 501;
const int MAX_M = 2501;
const int MAX_W = 201; struct Edge
{
int src, des, wei;
//Edge(int s, int d, int w) : src(s), des(d), wei(w) {}
}; Edge edge[(MAX_M<<1)+MAX_W];
int dist[MAX_N]; int N, M, W, F; bool cycleBellmanFord()
{
for (int i = 1; i <= N; i++) dist[i] = INT_MAX;
dist[1] = 0;
for (int i = 1; i < N; i++)
{
bool seperate = true;
for (int j = 0; j < (M<<1)+W; j++)
{
if (dist[edge[j].src] != INT_MAX &&
dist[edge[j].src]+edge[j].wei < dist[edge[j].des])
{
dist[edge[j].des] = dist[edge[j].src]+edge[j].wei;
seperate = false;
}
}
if (seperate) break;
}
for (int j = 0; j < (M<<1)+W; j++)
{
if ( dist[edge[j].src] != INT_MAX &&
dist[edge[j].src]+edge[j].wei < dist[edge[j].des]) return true;
}
return false;
} int main()
{
scanf("%d", &F);
while (F--)
{
scanf("%d %d %d", &N, &M, &W);
int i = 0;
for ( ; i < (M<<1); i++)
{
scanf("%d %d %d", &edge[i].src, &edge[i].des, &edge[i].wei);
i++;
edge[i].des = edge[i-1].src;
edge[i].src = edge[i-1].des;
edge[i].wei = edge[i-1].wei;
}
for ( ; i < (M<<1)+W; i++)
{
scanf("%d %d %d", &edge[i].src, &edge[i].des, &edge[i].wei);
edge[i].wei = -edge[i].wei;
}
if (cycleBellmanFord()) puts("YES");
else puts("NO");
}
return 0;
}

POJ 3259 Wormholes Bellman题解的更多相关文章

  1. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  2. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  3. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  4. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  5. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  6. poj 3259 Wormholes

    题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...

  7. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  8. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  9. poj 3259 Wormholes(最短路 Bellman)

    题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...

随机推荐

  1. js基本函数和基本方法

    日期时间函数(需要用变量调用): var b = new Date(); //获取当前时间 b.getTime() //获取时间戳 b.getFullYear() //获取年份 b.getMonth( ...

  2. MySQL-技术专区-数据库权限管理

    前言 学习mysql数据库,对于它的权限的管理是关键的一环.所以,下面介绍的是MySQL权限的管理. MySQL权限表 MySQL数据库实际上是通过将用户写入mysql库中对应的权限表来控制访问权限的 ...

  3. bzoj 3517翻硬币

    我们lv老师有点毒瘤啊... n为偶数... 离AC只差一张草稿纸233 挖个坑...

  4. STL_set

    #include <iostream> #include <set> #include <string> #include <cstdio> using ...

  5. 64位 __int 与 long long写法

    在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647.而unsig ...

  6. 【读书笔记】剑指offer

    导语 所有的编程练习都在牛客网OJ提交,链接: https://www.nowcoder.com/ta/coding-interviews 九章算法的 lintcode 也有这本书的题目.https: ...

  7. vue 初始化rem

    在assets => js => rem.js export default { init () { let sw = document.documentElement.clientWid ...

  8. java 获取String出现最多次数的字段

    package hello; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator ...

  9. Database - 数据库事务ACID

    总结 事务管理(ACID),谈到事务一般都是以下四点: 原子性(Atomicity)原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生.一致性(Consistency)事务前 ...

  10. Windows 屏幕保护程序

    { 创建一个win32 窗口项目,不是控制台的 把exe改为src文件 复制到windows目录下 ok }