POJ-3259 Wormholes(判断负环、模板)
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 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 题目大意:N条双向边,W条单向虫洞,走过每条边花费的时间是正的,走虫洞会让时间倒流,每条边和虫洞的花费已知,问小明从起点开始转一圈再回到起点的时间,能不能是在出发之前。
题目解析:用bellman_ford算法,判断是不是存在负环。 代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
const int INF=<<;
struct edge
{
int fr,to,w,nxt;
};
edge e[];
int head[],n,cnt,dis[];
void add(int u,int v,int w)
{
e[cnt].fr=u;
e[cnt].to=v;
e[cnt].w=w;
//e[cnt].nxt=head[u];
//head[u]=cnt++;
++cnt;
}
bool bellman_ford()
{
int i,j;
fill(dis,dis+n+,INF);
dis[]=;
for(i=;i<n;++i){
for(j=;j<cnt;++j){
if(dis[e[j].fr]!=INF&&dis[e[j].to]>dis[e[j].fr]+e[j].w){
dis[e[j].to]=dis[e[j].fr]+e[j].w;
if(i==n-)
return true;
}
}
}
return false;
}
int main()
{
int T,s,t;
scanf("%d",&T);
int a,b,c;
while(T--)
{
cnt=;
memset(head,-,sizeof(head));
scanf("%d%d%d",&n,&s,&t);
while(s--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
while(t--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,-c);
}
if(bellman_ford())
printf("YES\n");
else
printf("NO\n");
}
return ;
}
POJ-3259 Wormholes(判断负环、模板)的更多相关文章
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- poj 3259 Wormholes 判断负权值回路
Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...
- POJ 3259 Wormholes【最短路/SPFA判断负环模板】
农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...
- POJ3259(Wormholes) 判断负环
题意: 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W个 虫洞组成,FJ想从一块土地开始,经过若干条路和虫洞 ...
- Wormholes---poj3259(最短路 spfa 判断负环 模板)
题目链接:http://poj.org/problem?id=3259 题意是问是否能通过虫洞回到过去: 虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts. 我们把虫洞看成是一条负权路,问 ...
- POJ 3259 Wormholes Bellman_ford负权回路
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes(负权环路)
题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
随机推荐
- POI Excel文件的读取与写入
1. 创建目录 if(!(new File(path).isDirectory())){ new File(path).mkdirs();} 2. 读取Excel文件,并进行写入操作 Workbook ...
- Python Web笔记之高性能网络编程
请参考博客: https://blog.csdn.net/russell_tao/article/details/9111769
- MySQL数据库 基本操作语句
操作MySQL数据库 1.创建数据库 create database 数据库名: 2.查看数据库 show databases: 3.选择指定数据库 use 数据库名: 4.删除数据库 drop da ...
- 01: 重写Django admin
目录: 1.1 重写Django admin项目各文件作用# 1.2 重写Django admin用户认证 1.3 将要显示的表注册到我们自己的kind_admin.py中 1.4 项目首页:显示注册 ...
- 开发代码code中变量替换
除了automake/autoconfig 之外,还有其他的替换方式. 参看vdsm https://github.com/oVirt/vdsm/blob/master/Makefile.am htt ...
- 串口WIF简单I调试
串口WIF简单I调试 /*********************************************************************** Title:Wifi串口调试 H ...
- 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 2 章 答案
判断对错1.编写程序的好方法是立即键入一些代码,然后调试它,直到它工作.2.可以在不使用编程语言的情况下编写算法.3.程序在写入和调试后不再需要修改.4.Python 标识符必须以字母或下划线开头.5 ...
- 平衡树之伸展树(Splay Tree)题目整理
目录 前言 练习1 BZOJ 3224 普通平衡树 练习2 BZOJ 3223 文艺平衡树 练习3 BZOJ 1588 [HNOI2002]营业额统计 练习4 BZOJ 1208 [HNOI2004] ...
- redis安装使用配置
一.安装前的准备 下载redis http://redis.io/download https://github.com/mythz/redis-windows 下载Windows版客户端net版sd ...
- [luogu 3957]跳房子
题目链接 50分做法 挺显然的一个做法,因为金币量是单调的(如果你花i枚金币可以得到最优解,i+1枚也一定可以),所以可以二分答案 然后对于二分出来的每个答案,都做一遍dp,效率$O(n^2logn) ...