Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 34934   Accepted: 12752

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.
这题使用Bellman-Ford算法
 #include <iostream>
using namespace std;
struct farm {
int S;
int E;
int T;
} f[];
int main() {
int num;
int N, M, W;
cin >> num;
int F[];
for (int i = ; i < num; i++) {
cin >> N >> M >> W;
for (int j = ; j < N; j++) {
F[j] = ;
}
F[] = ;
for (int j = ; j < M; j++) {
int a, b, c;
cin >> a >> b >> c;
f[*j].S = a;
f[*j].E = b;
f[*j].T = c;
f[*j+].S = b;
f[*j+].E = a;
f[*j+].T = c; }
for (int j =* M; j < *M + W; j++) {
int a, b, c;
cin >> a >> b >> c;
f[j].S = a;
f[j].E = b;
f[j].T = - c;
}
for (int j = ; j < N-; j++) {
for (int k = ; k < *M + W; k++) {
if (F[f[k].E] > F[f[k].S] + f[k].T) {
F[f[k].E] = F[f[k].S] + f[k].T;
}
}
}
int flag = ;
for (int k = ; k < *M + W; k++) {
if (F[f[k].E] >F[f[k].S] + f[k].T) {
F[f[k].E] = F[f[k].S] + f[k].T;
flag=;
break;
}
}
if(flag){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return ;
}

Wormholes - poj 3259 (Bellman-Ford算法)的更多相关文章

  1. Bellman—Ford算法思想

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

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

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

  3. (最短路 spfa)Wormholes -- poj -- 3259

    http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions ...

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

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

  5. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  6. poj 3259 bellman最短路推断有无负权回路

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36717   Accepted: 13438 Descr ...

  7. ShortestPath:Wormholes(POJ 3259)

    田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...

  8. poj 3259(bellman最短路径)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30169   Accepted: 10914 Descr ...

  9. kuangbin专题专题四 Wormholes POJ - 3259

    题目链接:https://vjudge.net/problem/POJ-3259 思路:求有无负环,起点随意选就可以,因为目的只是找出有没有负环,有了负环就可以让时间一直回退,那么一定能回到当初,这里 ...

随机推荐

  1. Broken Code

    给一个sorted array 0 0 0 1 1 1 1,然后找出第一个1的位置. 边界情况:array为空或者全0. 思路:二分查找.为了优化,可以先判断最后一个数是不是0. class Solu ...

  2. redis --- lua 脚本实现原子操作

    如题, 楼主的想法很简单, lua 脚本本身支持原子性, 所以把命令写进一个脚本就行, 当然后续还会优化才能放到生产上,例如缓存脚本 ,redis 本身会缓存执行过的脚本 ,这样速度更快, 再优化, ...

  3. (转)Unity3d通过Action注册事件,回调方法

    http://www.cnblogs.com/jisi5789/archive/2013/04/22/3036589.html using UnityEngine; namespace Liulala ...

  4. 【NOIP模拟赛】【数学真奇妙】【递推】旅行者问题

    旅行者问题 [问题描述] lahub是一个旅行者的粉丝,他想成为一个真正的旅行者,所以他计划开始一段旅行.lahub想去参观n个目的地(都在一条直道上).lahub在起点开始他的旅行.第i个目的地和起 ...

  5. Mac上Git的安装与简单使用

    一.安装: Git下载地址: http://git-scm.com/downloads/ 下载Git.配置Git: http://blog.csdn.net/reactor1379/article/d ...

  6. linux-shell父子进程

          用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令声明变量,也可以创建并运行 shell脚本程序.运行shell脚本程序时,系统将创建一个 ...

  7. 服务器不安装Excel,实现导出Excel功能

    /// <summary> /// 导出为Excel /// </summary> /// <param name="sender"></ ...

  8. Delphi创建开机启动项的方法示例

    Delphi可以通过创建开机启动项键值的方法,将程序添加到开机启动项中.通过本实例代码就可以为您的程序添加到快速启动中,随着Windows一起启动,开机即运行的程序.该实例代码简单,主要是通过添加注册 ...

  9. apache的动态和静态

    apache的动态和静态  http://www.cnblogs.com/eoiioe/archive/2008/12/23/1360476.html(2.0和2.2一样) 关于apache的动态与静 ...

  10. squid.con 配置文件详解

    博客转载:http://www.articleswriting.net/article/6477447043/;jsessionid=42C9702B475ECF99EB861214186390E8 ...