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. Ugly Number II -- LeetCode

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  2. Lowest Common Ancestor of a Binary Search Tree -- LeetCode

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. [Luogu1429]平面最近点对(加强版)

    题目大意: 平面最近点对. 思路: 分治. 首先将所有点排序 每次把当前区间分为两半,递归求解两个区间内部的情况,然后枚举区间两边的点. #include<cmath> #include& ...

  4. SONY的一款Win8平板

    今天看到了SONY新发布的一款x86的平板电脑: 铝合金的机身,分离的屏幕,非常漂亮.参数上还是很给力的,i5-4210/i7-4610的处理器,1920x1080的屏幕.4G的内存.9.9mm的厚度 ...

  5. TQ2440平台上LCD驱动的移植

    参考: http://liu1227787871.blog.163.com/blog/static/205363197201242393031250/ http://blog.csdn.net/cum ...

  6. MySQL集群---②Windows平台搭建MySQL CLUSTER集群

    原文:http://blog.csdn.net/mazhaojuan/article/details/42211857 本文将通过两台电脑来简单介绍一下Windows平台如何搭建MySQL集群. My ...

  7. 【微信】2.微信小程序开发--官方开发工具使用说明

    承接第一篇 =============================================== 关于微信小程序开发使用IDE,曾经自己动摇过. 到底是采用 微信官方小程序开发工具 WebS ...

  8. Vue的常用指令v-if, v-for, v-show,v-else, v-bind, v-on

    Vue.js的指令是以v-开头的,它们作用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTML特性(attribu ...

  9. linux /etc/hosts 配置问题

    在java code中获取本机IP的程序如下: import java.net.InetAddress; public class Test { public static void main(Str ...

  10. ECSHOP热门搜索关键词随机显示

    实现ECSHOP热门搜索关键词随机显示,需要修改ECSHOP模板和ECShOP程序,按照步骤修改即可. 一.打开 include/lib_main.php 文件,找到下面这段代码,删除之(大概在165 ...