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. 一个优秀的Android应用从建项目开始

    1.项目结构 现在的MVP模式越来越流行.就默认采用了.如果项目比较小的话: app——Application Activity Fragment Presenter等的顶级父类 config——AP ...

  2. 怎样开发单页面app

    groot.view("myview", function (vm, ve) { ve.loada = function () { require("./app/page ...

  3. Swift与Objective-c 混编CocoaPods 引入第三方库遇到的问题 (一)

    最近Swift 这么火也想尝试着用一下.考虑到Swift 出来的时间也不长.还有就是就是苹果更新的过于平凡 暂时还是不要将现有项目都用swift开发. 先来看看我遇到的问题: 问题一.

  4. Provider 错误 '80004005' 未指定的错误 /conn.asp,行 23

    网站本来正常,由于网站修改了title标签的内容,想要百度快照快点更新. 就自己提交快照.在下面网站: http://zhanzhang.baidu.com/sitesubmit/index 提交UR ...

  5. 一头扎进EasyUI

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 一头扎进EasyUI第1讲 .加载库文件和样式 <link rel="stylesheet" ...

  6. JS BOM知识整理

    BOM部分主要是针对浏览器的内容,其中常用的就是window对象和location, window是全局对象很多关于浏览器的脚本设置都是通过它. location则是与地址栏内容相关,比如想要跳转到某 ...

  7. ORACLE创建表空间、创建用户、更改用户默认表空间以及授权、查看权限

    Oracle创建用户.表空间.导入导出....命令 //创建临时表空间 create temporary tablespace ext_temptempfile 'D:\oracle\product\ ...

  8. 蝙蝠算法-python实现

    BAIndividual.py import numpy as np import ObjFunction class BAIndividual: ''' individual of bat algo ...

  9. CentOS下crontab执行java程序

    阿里云CentOS收不到邮件 在crontab里配置执行脚本,脚本用来执行java程序,死活不执行.单独执行脚本可以运行. 查看crontab的日志文件,/var/log/cron,发现没有收到cro ...

  10. easyVS

    easyVS 详细说明点这里