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. 结构体mem_pool_t

    /** Memory area header */ typedef struct mem_area_struct mem_area_t; /** Memory pool */ typedef stru ...

  2. IIS大数据请求设置方法

    大并发大数据量请求一般会分为几种情况: 1.大量的用户同时对系统的不同功能页面进行查找,更新操作 2.大量的用户同时对系统的同一个页面,同一个表的大数据量进行查询操作 3.大量的用户同时对系统的同一个 ...

  3. 图表框架HelloCharts(3)饼状图

    1 效果图 2 xml文件 activity_pie_chart.xml <FrameLayout xmlns:android="http://schemas.android.com/ ...

  4. 两个android程序间的相互调用(apk互调)

    通常我们用到的只是activity之间的互相跳转和调用,很少会用到apk级别的互相调用. 往往在一些应用上会用到,比如一个支付系统,可能会有很多的一系列的程序调用到:彩票系统.订票系统.团购网……全部 ...

  5. Informatica元数据库解析

    Informatica所有的元数据信息均以数据库表的方式存到了元数据库中.当然Infa本身工具提供了很多的人性化的功能,使我们在开发时可以很方便的进行操作,但人们的需求总是万变的,需要方便的取到自己需 ...

  6. 【c++内存分布系列】单独一个类

    首先要明确类型本身是没有具体地址的,它是为了给编译器生成相应对象提供依据.只有编译器生成的对象才有明确的地址. 一.空类 形如下面的类A,类里没有任何成员变量,类的sizeof值为1. #includ ...

  7. Linux Kernel 4.8分支第4个候选版本发布

    导读 今天,大神Linus Torvalds宣布了Linux 4.8分支的第四个候选版本,该候选版本在提供常规驱动更新.架构改善和部分KVM调整之外最大的新功能就是修复了英特尔Skylake电源管理B ...

  8. mysql show processlist命令 详解

    SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...

  9. HDU 5727 Necklace 环排+二分图匹配

    这是从山东大学巨巨那里学来的做法 枚举下黑色球的排列总数是8!,然后八个白球可选的位置与左右两个黑球存不存在关系建图就行 这是原话,具体一点,每次生成环排,只有互不影响的才连边 最后:注重一点,n个数 ...

  10. OpenGL学习——基本概念和坐标变换

    基本概念 基本功能:几何图形.变换.着色.光照.贴图 高级功能:曲面图元.光栅操作.景深.shader编程   状态机 先设置状态参数:多边形.顶点列表.填充颜色.纹理.混合模式.坐标系 再调用绘图指 ...