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. js 浏览器 宽高 各种

    常用: JS 获取浏览器窗口大小   // 获取窗口宽度   if (window.innerWidth)   winWidth = window.innerWidth;   else if ((do ...

  2. coursera-斯坦福-机器学习-吴恩达-笔记week2

    1 多元线性回归 1.1 假设函数 多元线性回归是指有多个特征特征变量的情况.此时我们修改假设函数hθ(x)=θ0+θ1∗x为hθ(x)=θ0+θ1x1+θ2x2+⋯+θnxn.设x0=1,x为特征向 ...

  3. [HAOI2007]反素数

    这道题其实就是求在 [1,n] 的区间内,那个数的约数个数最多,如果同样多,取最小... 那么我们只需要把质因数分解反过来做,然后更新答案就好了... 素数不需要筛出来,直接打表就好,因为只能用到几个 ...

  4. OO课程中IDEA相关插件的使用

    写在前面   由于OO课程博客作业的需要分析代码的复杂度并绘制UML图,但是课件上推荐的分析工具(http://metrics.sourceforge.net )经过自己几个小时的折腾还是没有安装成功 ...

  5. Codeforces Round #309 (Div. 1) A(组合数学)

    题目:http://codeforces.com/contest/553/problem/A 题意:给你k个颜色的球,下面k行代表每个颜色的球有多少个,规定第i种颜色的球的最后一个在第i-1种颜色的球 ...

  6. asp.net mvc + dapper(ORM框架) + easyui框架简洁的信息管理项目

    1.目录结构: 2.效果图: 3.IndexController控制器: using System; using System.Collections; using System.Collection ...

  7. 创建一个vue 项目 必备的几个插件

    第一步npm安装 首先:先从nodejs.org中下载nodejs 打开控制命令行程序(CMD),node -v 检查是否正常 使用淘宝NPM 镜像 npm  install  -g  cnpm  - ...

  8. java和js中JSONObject,JSONArray,Map,String之间转换

    --------------------------------------------------Java中--------------------------------------------- ...

  9. Linux下截屏方法

    参考百度经验 https://jingyan.baidu.com/article/48a42057c8e8dfa92525047c.html 第一种: 截屏部分画面并保存 快捷键Shift+PrtSc

  10. PyCharm介绍与基础操作

    PyCharm是一种Python IDE(集成开发软件),带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具. 1.1 PyCharm主界面 PyCharm包含4个主要区分,分别为菜单 ...