题目网址: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. 一步步到IOC

    一段代码 class Program { static void Main(string[] args) { var shop=new Shop(); shop.Add(); shop.Delete( ...

  2. C++性能查看-宏定义输出

    之前由于想统计代码中每个模块加载时长,因此写了一个模块加载时长统计类,使用起来也是超级方便,只需要定义一个宏即可 使用方式如下: 1.统计函数性能 void func() { CONSUMING_OU ...

  3. 50 (OC)* URL Scheme 网页地址协议

    在Xcode 9 下,新建的工程,在plist文件中注册URL Schemes,从safari无法打开问题 1:URL Scheme是什么 2:URL Scheme有什么作用 3:URL Scheme ...

  4. java线上cpu、内存问题排查方法

    一.线程 查进程中占用cpu高的线程 ps -mp xxxxx -o THREAD,tid,time | sort -rn 将线程的id从10位转到16位,可以在下面jstack中找到对应线程 输出线 ...

  5. php数据提交POSt

    通常情况下用户使用浏览器网页表单向服务器post提交数据,我们使用PHP的$_POST接收用户POST到服务器的数据,并进行适当的处理.但有些情况下,如用户使用客户端软件向服务端php程序发送post ...

  6. (七十五)c#Winform自定义控件-控件水印组件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  7. Unity项目 - DeathtrapDungeon死亡地牢

    目录 游戏原型 项目演示 绘图资源 代码实现 注意事项 技术探讨 参考来源 游戏原型 死亡地牢是一款 2D-Roguelike 的地牢冒险游戏.手握利刃,斩杀怪物,在凶险的地牢内生存下去.但注意,敌人 ...

  8. Java面向对象笔记(五分钟秒懂)

    面向对象概念 面向对象三大特征:封装,继承,多态 面向对象编程(OOP,Object Oriented Programing)是相对于面向过程编程说的,之前写的代码基本都是纯的面向过程编程的,当项目复 ...

  9. idea 自动生成并跳转单元测试

    在要测试的类上按快捷键ctrl + shift + t,选择Create New Test,在出现的对话框的下面member内勾选要测试的方法,点击ok 或者点击菜单栏Navigate–>tes ...

  10. Angular 自定义管道

    管道的作用就是将原始值进行转化处理,转换为所需要的值: 1. 新建sex-reform.pipe.ts文件 ng g pipe sex-reform 2. 编辑sex-reform.pipe.ts文件 ...