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. 各浏览器各版本User-agent汇总 欢迎补充

    Internet Explorer Internet Explorer 5 Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; WOW64; Trident/ ...

  2. android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法

    这篇文章介绍了android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法,有需要的朋友可以参考一下 布局文件中的TextView属性 复制代码代码如下: < ...

  3. 《分销系统-原创第一章》之“多用户角色权限访问模块问题”的解决思路( 位运算 + ActionFilterAttribute )

    此项目需求就是根据给用户分配的权限,进行相应的权限模块浏览功能,因为项目不是很大,所以权限没有去用一张表去存,我的解决思路如下,希望大家给点建议. 数据库用户表结构如下: 数据库表梳理: BankUs ...

  4. UVA 11865 Stream My Contest(最小树形图)

    题意:N台机器,M条有向边,总资金C,现要到搭建一个以0号机(服务器)为跟的网路,已知每条网线可以把数据从u传递到v,其带宽为d,花费为c,且d越大,传输速度越快,问能够搭建的传输速度最快的网络d值是 ...

  5. [反汇编练习] 160个CrackMe之016

    [反汇编练习] 160个CrackMe之016. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  6. OK335xS 网络连接打印信息 hacking

    /*********************************************************************** * OK335xS 网络连接打印信息 hacking ...

  7. MYSQL自动备份策略的选择

    目前流行几种备份方式: 1.逻辑备份:使用mysql自带的mysqldump工具进行备份.备份成sql文件形式.优点:最大好处是能够与正在运行的mysql自动协同工作,在运行期间可以确保备份是当时的点 ...

  8. java中时间格式yyyyMMddHHmmss的大小写问题

    字母     日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996 ; 96 M 年中的月份 Month July ; Jul ; 07 w 年中的周数 Numb ...

  9. ejabberd、jabber、jabberd、xmpp辨析

    Jabber 是著名的即时通讯服务服务器,它是一个自由开源软件,能让用户自己架即时通讯服务器,可以在Internet上应用,也可以在局域网中应用.    XMPP(可扩展消息处理现场协议)是基于可扩展 ...

  10. 从网页监听Android设备的返回键

    最近搞Android项目的时候,遇到一个比较蛋疼的需求,需要从Client App调用系统浏览器打开一个页面,进行杂七杂八的一些交互之后,返回到App.如何打开浏览器和如何返回App这里就不说了,有兴 ...