poj3259
| 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 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
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的更多相关文章
- POJ-3259 Wormholes---SPFA判断有无负环
题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...
- poj3259 Wormholes(Bellman-Ford判断负圈)
https://vjudge.net/problem/POJ-3259 一开始理解错题意了,以为从A->B一定得走路,B->A一定得走虫洞.emmm其实回来的时候可以路和虫洞都可以走,只要 ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】
题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...
- POJ3259 Wormholes 【spfa判负环】
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ-3259(最短路+Bellman-Ford算法判负圈)
Wormholes POJ-3259 这题是最短路问题中判断是否存在负圈的模板题. 判断负圈的一个关键就是理解:如果在图中不存在从s可达的负圈,最短路径不会经过一个顶点两次.while循环最多执行v- ...
- poj3259 spfa
spfa判断是否存在负环,path双向,wormhole单向
- poj3259 bellman——ford Wormholes解绝负权问题
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35103 Accepted: 12805 Descr ...
- poj3259 最短路判环
题意:有一些点.一些道路和一些虫洞,道路是双向的,连接两点,花费正的时间,而虫洞是单向的,连接两点,可以使时间倒退,求是否能够回到过去. 只要明确回到过去其实就是当出现一个负环的时候,不断沿这个环走, ...
随机推荐
- Request.Querystring中文乱码问题解决
现象:近期项目中用到查询字符串传值,如果传递的是英文一切正常,但是传递中文时,使用request.querystring[]得到的是乱码. 原因:不知道为什么,可能是编码不一致问题 解决方法1:修改w ...
- 解决魅族USB调试无法被电脑识别的问题(含Mac OS X、Win7)
每次打开豌豆荚或者360手机助手之类手机助手后Eclipse才会检测到mx4(实际上是豌豆荚关闭eclipse的adb使用自己的驱动连接的).解决方法就是在"adb_usb.ini&qu ...
- WDF模型驱动程序开发
WDF驱动程序开发 1. 引言 设备驱动程序是硬件设备连接到计算机系统的软件接口,任何设备都必须有相应的驱动程序才能在计算机系统上正常工作.设备驱动程序的优劣直接关系到整个系统的性能和稳定性,因此,设 ...
- linux下打开chm文件的方法
windows中,通常情况下,chm文件可以使用系统自带的程序打开,但是linux就没有那么幸运了,那么,如何在linux下打开chm 文件呢?有小编来为您介绍介绍,本篇,小编以ubuntu环境为例 ...
- Ruby 文件处理
#r read, #w write, #a append, #r+ 读写方式 从文件的头位置开始读取或写入, #w+ 读写方式,如果文件已存在清空该文件,不存在就创建一个新的文件, #a+ 如果文件存 ...
- JavaScript基础篇最全
本章内容: 简介 定义 注释 引入文件 变量 运算符 算术运算符 比较运算符 逻辑运算符 数据类型 数字 字符串 布尔类型 数组 Math 语句 条件语句(if.switch) 循环语句(for.fo ...
- [Papers]NSE, $u_3$, Lebesgue space [Jia-Zhou, NARWA, 2014]
$$\bex u_3\in L^\infty(0,T;L^\frac{10}{3}(\bbR^3)). \eex$$
- java多线程学习笔记——简单
进程:程序(任务)的执行过程——动态性. 持有资源(共享内存,共享文件)和线程. 线程:线程是系统中最小的执行单元,统一进程中有多个线程,线程共享进程的资源. 线程交互:互斥与同步. 注意:多线程是异 ...
- Redis Sentinel机制与用法
概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都 ...
- bzoj 1001: [BeiJing2006]狼抓兔子 平面图最小割
平面图跑最大流 可以转换为其对偶图跑最短路 一个环对应一个割 找到最小环(即最短路)极为所求,注意辅助边的建立 加入读入优化 不过时间还是一般 估计是dij写的不好 大神勿喷~~~ /*** ...