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.
 
『题解』
存在负权边时不能用Dijkstra算法求最短路。本题采用Bellman-Ford最短路算法判断负权回路。模板题冲冲冲。
 
『C++』
 #include <iostream>
#include <vector>
using namespace std;
#define INF 0x7fffffff int FieldsN, dist[]; struct Edge
{
int s, e;
int t;
Edge() {}
Edge(int _s, int _e, int _t) :
s(_s), e(_e), t(_t) {}
}; vector<Edge> edges; bool Bellmen_Ford(int v)
{
int s, e, t;
int Size = edges.size(); for (int k = ; k <= FieldsN; k++)
dist[k] = INF;
dist[v] = ;
for (int k = ; k < FieldsN; k++) {
for (int i = ; i < Size; i++) {
s = edges[i].s;
e = edges[i].e;
t = edges[i].t;
if (dist[s] != INF && dist[s] + t < dist[e])
dist[e] = dist[s] + t;
}
}
for (int i = ; i < Size; i++) {
s = edges[i].s;
e = edges[i].e;
t = edges[i].t;
if (dist[s] + t < dist[e]) return true;
}
return false;
} int main()
{
int F, M, W, S, E, T; cin >> F;
while (F--) {
edges.clear();
cin >> FieldsN >> M >> W;
while (M--) {
cin >> S >> E >> T;
edges.push_back(Edge(S, E, T));
edges.push_back(Edge(E, S, T));
}
while (W--) {
cin >> S >> E >> T;
edges.push_back(Edge(S, E, -T));
} if (Bellmen_Ford()) printf("YES\n");
else printf("NO\n");
} //system("pause");
return ;
}
 

POJ3259 Wormholes的更多相关文章

  1. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  2. poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】

    题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...

  3. POJ3259 Wormholes 【spfa判负环】

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

  4. POJ3259——Wormholes(Bellman-Ford+SPFA)

    Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...

  5. POJ--3259 Wormholes (SPFA判负环)

    题目电波   3259 Wormholes #include<iostream> #include<cstring> #include<algorithm> #in ...

  6. poj3259 Wormholes(Bellman-Ford判断负圈)

    https://vjudge.net/problem/POJ-3259 一开始理解错题意了,以为从A->B一定得走路,B->A一定得走虫洞.emmm其实回来的时候可以路和虫洞都可以走,只要 ...

  7. poj3259: Wormholes(BF模板题)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...

  8. poj3259 Wormholes【最短路-bellman-负环】

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

  9. POJ-3259 Wormholes(判断负环、模板)

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

随机推荐

  1. Delegate, NSNotification, KVO, Block

    delegate: 当我们第一次编写iOS应用时,我们注意到不断的在使用“delegate”,并且贯穿于整个SDK.delegation模式不是iOS特有的模式,而是依赖与你过去拥有的编程背景.针对它 ...

  2. unity5.x中的关节和布料

    关节 布料 关节 铰链关节(Hinge     Joint):将两个物体以链条的形式绑在一起,当力量过大超过链条的固定力矩时,两个物体就会产生相互的拉力. 固定关节(Fixed     Joint): ...

  3. HTML DOM 属性

    innerHTML 属性 获取元素内容的最简单方法是使用 innerHTML 属性. innerHTML 属性对于获取或替换 HTML 元素的内容很有用. 实例 下面的代码获取 id="in ...

  4. windows Jenkins git 配置

    待更新 插件下载地址:http://updates.jenkins-ci.org/download/plugins/ 参考地址:https://blog.csdn.net/zzy1078689276/ ...

  5. mpvue 转小程序实践总结

    介绍 Mpvue 是一个使用 Vue.js 开发小程序的前端框架.  基础介绍 框架基于 Vue.js 核心,修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运行在小程序 ...

  6. __FILES__

    _FILE_ :被称为PHP魔术常量 ,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径 1)dirname(__FILE___) 函数返回的是脚本所在在的路径.   比如文件 b.php ...

  7. find语法

    语法 find path -option [ -print ] [ -exec -ok command ] {} \; 参数说明 : find 根据下列规则判断 path 和 expression,在 ...

  8. huffman(greedy)

    present a file by binary character code,let the less characters can be presented simplier. package g ...

  9. MySQL常用内置函数

    本篇博客源自以下博客地址: http://www.mamicode.com/info-detail-250393.html

  10. #20175120彭宇辰 java第五周学习总结

    第六章 接口与实现 教材学习内容总结 接口-接口声名interace -接口体1.只有常量声明和抽象方法2.所有常量和方法的访问权限都为public3.常量都为static常量4.可省略pulic\s ...