Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36425   Accepted: 13320

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..N, M (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, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W

Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) 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 (S, E, T) 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.

Source

题意:判断是否有负环。
收获:bellmanFord:松弛n-1轮,每次把所有边都用上,如果还存在 dis[edge[j].v] > dis[edge[j].u] + edge[j].w 则有负环。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0); const int maxn=+;
int n;
struct Edge
{
int u, v, w, next;
};
Edge edge[maxn];
int num;
int head[maxn];
void init_edge()
{
num = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int w)
{
edge[num].u = u;
edge[num].v = v;
edge[num].w = w;
edge[num].next = head[u];
head[u] = num++;
}
int dis[maxn];
bool bellmanFord()//bellmanFord模板
{
for(int i = ; i <= n; i++) dis[i] = INF;
dis[] = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < num; j++)
{
if(dis[edge[j].v] > dis[edge[j].u] + edge[j].w)
dis[edge[j].v] = dis[edge[j].u] + edge[j].w;
}
}
//bool flag = 1;
for(int j = ; j < num; j++)
{
if(dis[edge[j].v] > dis[edge[j].u] + edge[j].w)
return ;
}
return ;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int m ,w;
scanf("%d%d%d",&n, &m, &w);
int a, b, c;
init_edge();
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &a, &b,&c);
addedge(a, b, c);
addedge(b, a, c);
}
for(int i = ; i < w; i++)
{
scanf("%d%d%d", &a, &b,&c);
addedge(a, b, -c);
}
//int flag = 0;
if(!bellmanFord())
printf("YES\n");
else
printf("NO\n");
}
return ;
}

POJ 3259 Wormholes( bellmanFord判负环)的更多相关文章

  1. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  2. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  3. Wormholes POJ 3259(SPFA判负环)

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

  4. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  5. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  6. POJ 1860 Currency Exchange (bellman-ford判负环)

    Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...

  7. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  8. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  9. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

随机推荐

  1. 04747_Java语言程序设计(一)_第6章_图形界面设计(二)

    例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...

  2. 用dTree组件生成无限级导航树

     在做管理系统时不可避免要用到导航树,这种东西只要一次做好,就可以随处运行,目前比较好的组件是dTree,原则上可以达到无限级,当然实际运行中4,5级就已经很多了,dTree的速度还是不错的,而且是J ...

  3. ssh公私钥登录方式设置

    在Linux中ssh登录远程主机的时候能够进行公私钥的认证方式. ①环境说明:两台Linux主机,host1:192.168.5.1,host2:192.168.5.10. 如今在host1上面设置然 ...

  4. 男同胞爱小秘籍--作为爱他的女朋友了几天C规划

    各位男同胞,不知道你的女朋友没有在过去的一问天,你这个问题~~ 场景重现: 女友:"今天天气不错." 你们:"对" 女友:"今天是我们知道它的最初几天 ...

  5. 一个用 Cumulative Penalty 培训 L1 正规 Log-linear 型号随机梯度下降

      Log-Linear 模型(也叫做最大熵模型)是 NLP 领域中使用最为广泛的模型之中的一个.其训练常採用最大似然准则.且为防止过拟合,往往在目标函数中增加(能够产生稀疏性的) L1 正则.但对于 ...

  6. BOOST 线程完全攻略 - 基础篇

    http://blog.csdn.net/iamnieo/article/details/2908621 2008-09-10 12:48 9202人阅读 评论(3) 收藏 举报 thread多线程l ...

  7. hdu 2612 Find a way(BFS)

    题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...

  8. Adnroid Studio使用技巧

    官方第一条提示:所有的使用技巧都可以通过Help→Tips of the Day查看. 下面摘抄一些比较有用的技巧: 1.Esc把活动窗口从工具窗口指向编辑窗口.F12把编辑窗口指向上一次活动的工具窗 ...

  9. PHP学习笔记三十八【下载】

    <?php //演示下载一个图片 $file_name="SunSet.jpg"; $file_name=iconv("utf-8","gb23 ...

  10. iOS 判断设备是否越狱

    我们在开发过程中,需要知道设备是否越狱,在网上查看很多资料,为此封装一些判断的方法. 上代码,不解释: .h文件 #import <Foundation/Foundation.h> @in ...