题目网址: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. 浅谈DanmakuView

      今天简单介绍一下开源的弹幕引擎---danmakuView   使用之前在build.gradle里面添加下面这一条(目前我使用的工具是AndroidStudio 3.1.2)   impleme ...

  2. [Advanced Python] 13 - "Decorator": syntactic sugar

    单独一章复习:Python 函数装饰器,廖雪峰讲装饰器 基本概念 函数作为变量 从函数中返回函数 子函数写在外面貌似也可以,可这样就少了“封装性”. def greet(): return " ...

  3. [Leetcode] 第313题 超级丑数

    一.题目描述 编写一段程序来查找第 n 个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13, ...

  4. 极光推送JPush

    推送ios以及android信息,简单的基于jpush v2带IMEI的推送实现. maven: <dependency> <groupId>cn.jpush.api</ ...

  5. sql server 建表,主键与外键约束

    主键: 能唯一区分表中每一行 外键:为某表的一列,是另一个表的主键,外键定义了两表之间的联系 商品类别表 use eshopgocreate table category( name varchar( ...

  6. 品Spring:能工巧匠们对注解的“加持”

    问题的描述与方案的提出 在Spring从XML转向注解时,为了自身的开发方便,对注解含义进行了扩充(具体参考本号上一篇文章). 这个扩充直接导致了一个问题,就是需要从注解往元注解以及元元注解(即沿着从 ...

  7. 手把手创建gulp

    这几天安装gulp踩了不少坑,现在讲解一个入门的案例解析: ==首先大家要确保node.npm.npx.gulp安装是否成功 == 这些安装都是傻瓜式安装,大家可以找到相应的教材. 创建一个自己的文件 ...

  8. 推荐几个我近期排查线上http接口偶发415时用到的工具

    导读:近期有一个业务部门的同学反馈说他负责的C工程在小概率情况下SpringMvc会返回415,通过输出的日志可以确定是SpringMvc找不到content-type这个头了,具体为什么找不到了呢? ...

  9. DP动态规划———LCS最长公共子序列

    递推公式: ]==b[j-]) { dp[i][j]=dp[i-][j-]+; } else { dp[i][j]=max(dp[i-][j],dp[i][j-]); } 完整模板代码: int LC ...

  10. 解决thinkphp批量上传图片只有一张上传成功解决方案

    批量上传时 存在一个生成文件名的问题 如果出现此bug,则不要用原生的生成规则来命名图片文件名 如果你试试同时上传两个不同类型,例如一张jpg,一张png,你就发现的确是可以两张同时上传的! 方案1: ...