Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24864   Accepted: 8869

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 toF (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
#include<iostream>
#include<stdio.h>
using namespace std;
const int fMax = 505;
const int eMax = 5205;
const int wMax = 99999;
struct{
int sta, end, time;
}edge[eMax];
int point_num, edge_num, dict[fMax];
bool bellman_ford()
{
int i, j;
for(i = 2; i <= point_num; i ++)
dict[i] = wMax;//初始化
for(i = 1; i < point_num; i ++)//点要减1
{
bool finish = true; // 加个全部完成松弛的判断,优化了50多MS。
for(j = 1; j <= edge_num; j ++)
{
int u = edge[j].sta;
int v = edge[j].end;
int w = edge[j].time;
if(dict[v] > dict[u] + w)
{ // 松弛。
dict[v] = dict[u] + w;
finish = false;
}
}
if(finish) break;
}
for(i = 1; i <= edge_num; i ++)
{ // 是否存在负环的判断。
int u = edge[i].sta;
int v = edge[i].end;
int w = edge[i].time;
if(dict[v] > dict[u] + w) return false;
}
return true;
}
int main()
{
int farm;
scanf("%d", &farm);
while(farm --)
{
int field, path, hole;
scanf("%d %d %d", &field, &path, &hole);
int s, e, t, i, k = 0;
for(i = 1; i <= path; i ++)
{
scanf("%d %d %d", &s, &e, &t); // 用scanf代替了cin,优化了100多MS。
k ++;
edge[k].sta = s;
edge[k].end = e;
edge[k].time = t;
k ++;
edge[k].sta = e;
edge[k].end = s;
edge[k].time = t;
}
for(i = 1; i <= hole; i ++)
{
scanf("%d %d %d", &s, &e, &t);
k ++;
edge[k].sta = s;
edge[k].end = e;
edge[k].time = -t;
}
point_num = field;
edge_num = k;
if(!bellman_ford())
printf("YES\n");
else printf("NO\n");
for(i=0;i<=point_num;i++)
printf("%d ",dict[i]);
}
return 0;
}
												

poj3259的更多相关文章

  1. POJ-3259 Wormholes---SPFA判断有无负环

    题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...

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

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

  3. POJ3259 :Wormholes(SPFA判负环)

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

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

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

  5. POJ3259 Wormholes 【spfa判负环】

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

  6. POJ-3259(最短路+Bellman-Ford算法判负圈)

    Wormholes POJ-3259 这题是最短路问题中判断是否存在负圈的模板题. 判断负圈的一个关键就是理解:如果在图中不存在从s可达的负圈,最短路径不会经过一个顶点两次.while循环最多执行v- ...

  7. poj3259 spfa

    spfa判断是否存在负环,path双向,wormhole单向

  8. poj3259 bellman——ford Wormholes解绝负权问题

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 Descr ...

  9. poj3259 最短路判环

    题意:有一些点.一些道路和一些虫洞,道路是双向的,连接两点,花费正的时间,而虫洞是单向的,连接两点,可以使时间倒退,求是否能够回到过去. 只要明确回到过去其实就是当出现一个负环的时候,不断沿这个环走, ...

随机推荐

  1. ASP.NET MVC 学习6、学习使用Code First Migrations功能,把Model的更新同步到DB中

     参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-th ...

  2. LA 3263 (平面图的欧拉定理) That Nice Euler Circuit

    题意: 平面上有n个端点的一笔画,最后一个端点与第一个端点重合,即所给图案是闭合曲线.求这些线段将平面分成多少部分. 分析: 平面图中欧拉定理:设平面的顶点数.边数和面数分别为V.E和F.则 V+F- ...

  3. CodeForces 489B (贪心 或 最大匹配) BerSU Ball

    题意: 有m个男孩和n个女孩,每个人都有一个舞蹈熟练度,用一个不超过100的正整数来表示. 一个男孩和一个女孩能够结为舞伴当且仅当两人的熟练度相差不超过1. 问最多能结成多少对舞伴 分析: 这是一个二 ...

  4. UVa 11584 Partitioning by Palindromes【DP】

    题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...

  5. asp.net批量发布博客到各大博客平台

    新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/ap ...

  6. #include<unistd.h>头文件的理解

    1.百度百科定义 unistd.h 是 C 和 C++ 程序设计语言中提供对 POSIX 操作系统 API 的访问功能的头文件的名称.该头文件由 POSIX.1 标准(单一UNIX规范的基础)提出,故 ...

  7. ZOJ1311, POJ1144 Network

    题目描述:TLC电话线路公司正在新建一个电话线路网络.他们将一些地方(这些地方用1到N的整数标明,任何2个地方的标号都不相同)用电话线路连接起来.这些线路是双向的,每条线路连接2个地方,并且每个地方的 ...

  8. [Everyday Mathematics]20150104

    设 $a>0$, $$\bex x_1=1,\quad x_{n+1}=x_n+an\prod_{i=1}^n x_i^{-\frac{1}{n}}. \eex$$ 试证: $$\bex \vl ...

  9. Hadoop序列化

      遗留问题: Hadoop序列化可以复用对象,是在哪里复用的? 介绍Hadoop序列化机制 Hadoop序列化机制详解 Hadoop序列化的核心 Hadoop序列化的比较接口 ObjectWrita ...

  10. Delphi 使用串口模拟工具进行串口程序开发调试

      版权声明:本文为博主原创文章,如需转载请注明出处及作者. 本文由小李专栏原创,转载需注明出处:[http://blog.csdn.net/softwave/article/details/8907 ...