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. 2015-10-19 sql1

        SQL SERVER(一) 一.设置登陆验证 1.右键点击数据库->属性->安全性设置密码登陆 2.数据库下找到安全性->登录名->sa,右键点击sa->属性(修 ...

  2. 序列化模块— json模块,pickle模块,shelve模块

    json模块 pickle模块 shelve模块 序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. # 序列化模块 # 数据类型转化成字符串的过程就是序列化 # 为了方便存储和网 ...

  3. python中字符串格式化

    username='小黑'age=18high=1.88s2='欢迎%s,年龄是%d,身高是%.2f'%(username,age,high)#%s是通用的,%d就必须传整数,%f就必须是小数,想保留 ...

  4. [Leetcode 90]求含有重复数的子集 Subset II

    [题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets ...

  5. 内存泄漏 tensorflow

    http://blog.csdn.net/qq_25737169/article/details/78125550

  6. python接口自动化测试(一)-request模块

    urllib.request模块是python3针对处理url的. 1. 首先导入: from urllib import request 2. 构造url,构造url的headers信息和传参[re ...

  7. 前端修炼の道 | <div> 标签简介

    <div> 标签是最基本的,同时也是最常用的标签. 该标签是一个双标签,出现在主体区域中,主要作为一个容器标签来使用,在 <div> 标签中可以包含除 <body> ...

  8. 高级数据类型---元祖[tuple]

    一.Tuple(元组)与列表类似,不同之处在于元组的 元素不能修改,让列表不可以被修改,以保护数据安全 元组 表示多个元素组成的序列 元组 在 Python 开发中,有特定的应用场景 用于存储 一串 ...

  9. winform 写入txt

    StreamWriter sw; FileStream fs = new FileStream(@"D:\" + txtStringfield03.Text + ".tx ...

  10. sublime text 使用小技巧

    sublime下载各个版本 官网 插件官方网站地址 https://packagecontrol.io/ 一.安装设置字体及字体大小 1.点菜单“Preferences--->Setting - ...