版权声明:本文作者靖心,靖空间地址: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. PAT甲级——A1148 WerewolfSimpleVersion【20】

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  2. PAT甲级——A1144 TheMissingNumber【20】

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...

  3. java swing 中JTable实现指定单元格为下拉框

    利用自定义的CellEditor实现第四列第二行为下拉框,本列其余行为文本框 利用默认的DefaultCellEditor设置第五列整列为下拉框   package mypackage; import ...

  4. 【Elasticsearch 7 探索之路】(六)初识 Mapping

    上一篇主要讲解什么是 URL Search 和 Request Body Search 的语法.本篇对 Mapping 的 Dynamic Mapping 以及手动创建 Mapping 进行讲解. 1 ...

  5. 关于KiCAD

    关于KiCAD 使用背景 一直以来公司都在用AD作为EDA软件,但是最近公司频繁收到律师函,所以决定找一款开源的软件来代替AD,目前市场上开源的只找到了KiCAD和Easy EDA(中文版立创EDA) ...

  6. haproxy的代理中继用法

    背景: 现有: 一台baidu/ali/tecent云服务器A(地址为a.a.a.a); 一台VPS B(地址为b.b.b.b), B中搭建有ss,监听端口为8000. 需求: 一:使用A做跳板机访问 ...

  7. 前端学习(十二)js数据类型(笔记)

    选项卡:        for循环 for(初始值,条件,自增){}    for(var i=0; i<9;i++){} 几个按钮对应相同个内容!!! -------------------- ...

  8. 再也不用字符串拼接dom元素了

    <script type="text/html" id="tmp"> <div class="cla"> <u ...

  9. poj 2752 kmp的next数组

    题目大意: 求一个字符串中某一个既是前缀又是后缀的前缀的结尾下标: 基本思路: 从_next[len]开始找_next[_next[len]],再找_next[_next[_next[len]]],一 ...

  10. 【JavaWeb项目】一个众筹网站的开发(五)后台用户登录功能

    用户模块 1)注册 表单校验,使用校验插件 用户密码需要加密存储 注册成功后来到管理控制台,将用户放在session中,防止以后获取 以后用户经常获取用户id,使用mabatis主键自增策略,保存用户 ...