Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 34302   Accepted: 12520

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.
 
 
 
以每个点为起点跑一次,检测是否有负环。
 #include <iostream>
#include <cstdio>
using namespace std; const int INF = 0xfffffff;
const int SIZE = ;
int D[];
int N,M,W,F;
struct Node
{
int from,to,cost;
}G[SIZE]; bool Bellman_Ford(int);
bool relax(int,int,int);
int main(void)
{
scanf("%d",&F);
while(F --)
{
scanf("%d%d%d",&N,&M,&W);
int i = ;
while(i < * M)
{
scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
i ++;
G[i] = G[i - ];
swap(G[i].from,G[i].to);
i ++;
}
while(i < W + M * )
{
scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
G[i].cost = -G[i].cost;
i ++;
}
bool flag = true;
for(int i = ;i <= N;i ++)
if(!Bellman_Ford(i))
{
puts("YES");
flag = false;
break;
}
if(flag)
puts("NO");
} return ;
} bool Bellman_Ford(int s)
{
fill(D,D + ,INF);
D[s] = ;
bool update; for(int i = ;i < N - ;i ++)
{
update = false;
for(int i = ;i < M * + W;i ++)
if(relax(G[i].from,G[i].to,G[i].cost))
update = true;
if(!update)
break;
}
for(int i = ;i < M * + W;i ++)
if(relax(G[i].from,G[i].to,G[i].cost))
return false;
return true;
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}

Bellman_Ford

POJ 3259 Wormholes (最短路)的更多相关文章

  1. poj 3259 Wormholes(最短路 Bellman)

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

  2. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  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. ACM: POJ 3259 Wormholes - SPFA负环判定

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

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

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

  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_ford算法)

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

随机推荐

  1. cf754 A. Lesha and array splitting

    应该是做麻烦了,一开始还没A(幸好上一次比赛水惨了) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL lon ...

  2. Jquery 校验文本框只能输入负数、小数、整数

     /*   umlzhang    date:2013-09-12   */   //检验只能输入整数,小数和负数    $(function () {          var obj = $(&q ...

  3. 正确理解ContentPresenter

    下图显示继承关系: ContentControl:Control (在Control類並沒有Content屬性, 所以在這之上再寫了一個ContentControl, 使控件有Content屬性可以顯 ...

  4. SPSS二次开发

    在以前关于SPSS二次开发文章中留下过自己联系方式,差不多一年的时间,零零散散的和我取得联系的人也有几十位,看来对于SPSS二次开发的需求不少. Web SPSS系统是利用SPSS二次开发技术,使用户 ...

  5. visual studio 设计器上出现蓝色的点和箭头

    visual studio 设计器上出现蓝色的点和箭头: 突然发现打开visual studio 的时候出现了很多蓝色的点和箭头,解决办法是:按组合快捷键 Ctrl+R,Ctrl+W 或 Ctrl+E ...

  6. Git版本管理:Windows下Git配置与使用指南

    简要介绍:Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. 一.安装 软件:msysGit-fullinstall-1.8.1.2 打开之后设置安装路径,默认为 ...

  7. erlang pool模块。

    出自: http://blog.sina.com.cn/s/blog_96b8a154010168ti.html

  8. uva10327 - Flip Sort

    Flip Sort Sorting in computer science is an important part. Almost every problem can be solved effec ...

  9. Python 存储模型

    1.Python彻底分离了对象和引用,可以认为内存中的对象都是不可修改的,每次修改引用,相当于在堆上重新创建一个对象,引用指向新对象. 2.对于数值和字符串,修改意味着引用指向一个新对象. 3.集合中 ...

  10. JQueryMobile页面跳转参数的传递解决方案

    在JQueryMobile开发手机端应用使用可能需要考虑相关的页面跳转带来的参数问题.因为JQueryMobile其实也是HTML5实践的结果.HTML5中有localStorage和sessionS ...