Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 33008   Accepted: 12011

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.

Source

 #include<stdio.h>
#include<string.h>
#define MAX 0x3f3f3f3f
struct path
{
int u , v , t ;
}pa[]; int d[] ;
int n , m , w ;
int s , e , t ;
int f ;
int cnt ; bool Bellman_ford ()
{
for (int i = ; i <= n ; i++)
d[i] = MAX ;
d[] = ;
bool flag ;
for (int i = ; i <= n ; i++) {// ' = ' 不能省
flag = ;
for (int j = ; j < cnt ; j++) {
if (d[pa[j].v] > d[pa[j].u] + pa[j].t) {
flag = ;
d[pa[j].v] = d[pa[j].u] + pa[j].t ;
}
}
if (flag)
return true ;
}
return false ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
scanf ("%d" , &f) ;
while (f--) {
cnt = ;
scanf ("%d%d%d" , &n , &m , &w) ;
for (int i = ; i < m ; i++) {
scanf ("%d%d%d" , &s , &e , &t) ;
pa[cnt].u = s , pa[cnt].v = e , pa[cnt].t = t ;
cnt++ ;
pa[cnt].u = e , pa[cnt].v = s , pa[cnt].t = t ;
cnt++ ;
}
for (int i = ; i < w ; i++ , cnt++) {
scanf ("%d%d%d" , &s , &e , &t) ;
pa[cnt].u = s , pa[cnt].v = e , pa[cnt].t = -t ;
}
if (Bellman_ford())
puts ("NO") ;
else
puts ("YES") ;
}
return ;
}

Wormholes(Bellman-ford)的更多相关文章

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

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

  2. ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

    两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...

  3. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  4. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  5. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  6. POJ 3259 Wormholes Bellman题解

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

  7. poj3259 bellman——ford Wormholes解绝负权问题

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 Descr ...

  8. Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】

    题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...

  9. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  10. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

随机推荐

  1. java <? super Fruit>与<? extends Fruit>

    package Test2016; import java.util.ArrayList; import java.util.List; public class Test2016 { public ...

  2. ASP.NET MVC Controller Session问题

    发现问题 最近在项目中遇到这样一个问题,一直没办法重现,所以几天都没有解决. 测试那边给出的问题是这样的:每天早上来的时候,第一次通过单点登录到系统的时候,总会跳转回登录界面,再次登录就好了.当时给我 ...

  3. 用css画出对话框

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa4AAAFSCAYAAACqpTv4AAAgAElEQVR4nO3deZBU5b3GcUIlVTG3bi

  4. Bootstrap系列 -- 28. 下拉菜单状态

    下拉菜单项的默认的状态(不用设置)有悬浮状态(:hover)和焦点状态(:focus). 下拉菜单项除了上面两种状态,还有当前状态(.active)和禁用状态(.disabled).这两种状态使用方法 ...

  5. 4、面向对象以及winform的简单运用(继承与多态、命名空间与类库)

    继承 继承既子类与父类之间的关系.子类是父类的一种特例,子类拥有父类所没有的功能. 子类与父类之间的相互赋值——例: Parent p; Son c = new Son(); p = c; //正确, ...

  6. jQuery理解之(一)动画与特效

    本节主要降级和学习jQuery的自动显隐,渐入渐出.飞入飞出.自定义动画等. 1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,本节简单介绍jQuery的显示和隐藏 ...

  7. 读代码之htmlParser

    在以前使用HtmlParser时,并未考虑过遇到org.htmlparser.tags之外的Tag怎么处理.直到碰到这样的一个标签,如果不加处理,HtmlParser无法对其进行处理.查阅自定义标签之 ...

  8. 转:Oracle中merge into的使用

    最近项目上使用Oracle的Merge,所以找来一下资料学习了解. 该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. ORACLE 9i 中,使用此命令必须同时指定UPDATE 和I ...

  9. requirejs

    //index.html <!doctype html> <html> <head> <meta charset="utf-8"> ...

  10. c++重载运算符注意

    c++重载运算符的时候加&或不加: 如果加了&表示引用,说明用的都是同一块内存.如果不加,那么用的就是一份拷贝,即不同的内存. 一般连续操作的时候要加&. 可以重新定义一个对象 ...