题目网址:http://poj.org/problem?id=3259

题目:

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 52198   Accepted: 19426

Description

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..NM (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input

Line 1: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path. 
Lines M+2..M+W+1 of each farm: Three space-separated numbers (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

Hint

For farm 1, FJ cannot travel back in time. 
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
 
思路:

我们根本不需要关心他所处的起点的具体位置,我们只需要判断是否有负权环即可,所以将所有的dist[i]都初始化为无穷大。有负权环的话就输出YES,没有的话就输出NO。很自然地就会想到Bellman-Ford算法。判断第n次循环,是否还会松弛,如果还需要就说明有负权环。 这道题需要注意的一点是:虫洞是单向边,路径是双向边。
 
代码:
 #include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = ;
struct node{
int v,u,w;
};
vector<node>v;
int n,m,w;
int dist[];
node x;
bool relax(int j){//松弛操作
if(dist[v[j].u]>dist[v[j].v]+v[j].w){
dist[v[j].u]=dist[v[j].v]+v[j].w;
return true;
}
return false;
}
bool bellman_ford(){
for (int i=; i<=n; i++) {
dist[i]=inf;
}
for (int i=; i<n-; i++) {
int flag=;
for (int j=; j<v.size(); j++) {
if(relax(j)) flag=;
}
if(!flag) return false;
}
for (int j=; j<v.size(); j++) {//核心
if(relax(j)) return true;
}
return false;
}
int main(){
int t;
cin>>t;
while (t--) {
int ok=;
v.clear();
cin>>n>>m>>w;
for (int i=; i<m; i++) {
cin>>x.v>>x.u>>x.w;
v.push_back(x);
swap(x.v, x.u);
v.push_back(x);
}
for (int i=; i<w; i++) {
cin>>x.v>>x.u>>x.w;
x.w=-x.w;
v.push_back(x);
}
if (bellman_ford()) printf("YES\n");
else printf("NO\n");
}
return ;
}

POJ 3259 Wormholes(Bellman-Ford)的更多相关文章

  1. POJ 3259 Wormholes Bellman题解

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

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

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

  3. 最短路(Bellman_Ford) POJ 3259 Wormholes

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

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

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

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

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

  6. POJ 3259 Wormholes (Bellman_ford算法)

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

  7. poj 3259 Wormholes

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

  8. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

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

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

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

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

随机推荐

  1. js中Math对象常用的属性和方法

    1 Math对象 1.1定义:Math是js的一个内置对象,它提供了一些数学方法. 1.2特性:不能用构造函数的方式创建,无法初始化,只有静态属性和方法 1.3静态属性 1.3.1 Math.PI 圆 ...

  2. Docker学习之docker架构

    docker架构 解释 1.docker命令提交给docker daemon进行处理,可以拖取镜像,运行容器等等. 2.最右边的实际上是互联网的sass服务,docker daemon可以和Regis ...

  3. [Linux] CentOS安装GNOME时,fwupdate-efi-12-5.el7.centos.x86_64 conflicts with grub2-common-1:2.02-0.65.el7.centos.noarch

    参考文章:https://createdpro.com/a/100006 该问题源于文件的版本冲突: grub2-common包的冲突,所以要将该包使用yum update grub2-commonn ...

  4. 讨厌的Permission denied:adb访问手机目录时,怎么处理Permission denied问题

    故事背景 手机某app出现了无响应,我想找到手机anr日志 但我只知道在data目录的某个目录里有个tra**的文件里有anr日志 具体的我真忘了,所以想要进入data中用ls查看一下 结果就出现了讨 ...

  5. JAVA设计模式---总述篇

    一.设计模式(Design Pattern): 1.设计模式的概念 是前辈们对代码开发经验的总结,是解决特定问题的一系列套路.它不是语法规定,而是一套用来提高代码可复用性.可维护性.可读性.稳健性以及 ...

  6. Cocos Creator实现左右跳游戏

    ​1. 玩法说明 游戏开始后,点击屏幕左右两侧,机器人朝左上方或右上方跳一步,如果下一步有石块,成功得1分,否则游戏结束. 2. 模块介绍 游戏场景分为2个:主页场景(home).游戏场景(game) ...

  7. [经验栈]SQL语句逻辑运算符"AND"、"&&"兼容性

    最近打算把博客转移到typecho平台,选了一个风格个人比较喜欢的主题,即Akina for Typecho 主题模板,在这里先感谢题主的开源分享,但是在使用过程中一开始就出现"500 Da ...

  8. HashMap和Hashtable的联系和区别

    实现原理相同,功能相同,底层都是哈希表结构,查询速度快,在很多情况下可以互用,早期的版本一般都是安全的. HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们之间的分 ...

  9. linux mint 17编译android 2.3.1错误记录

    有转载这里的也有添加的. ################# Fix 1 ########################## Error: frameworks/base/include/utils ...

  10. springboot 集成Redis单机

    1.redis服务搭建 centos7 搭建redis服务 2.接入相关 pom文件依赖引入 <dependencies> <dependency> <groupId&g ...