POJ 3259 Wormholes (判负环)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 46123 | Accepted: 17033 |
Description
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
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
Hint
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving
back at his starting location 1 second before he leaves. He could start from
anywhere on the cycle to accomplish this.
Source
题目大意:
FJ有n块农场,编号为1到n,这n块农场由m条道路和w个虫洞连接,没条道路是双向的,权值为t1,表示经过每条道路需要花费t1个时间单位,每个虫洞是单向的,权值为t2,经过每个虫洞可以让你回到t2个时间单位之前(说白了就是时光倒流);现在问你,FJ想从1号农场开始,经过若干农场后,在其出发之前的某一时刻回到1号农场。现在问你能否实现。
分析:
我们把虫洞上的权值看成负的,这样问题就变成了问你这块农场中是否存在负环的问题了。
单纯spfa跑最短路,判一个点入队超过n次,就有负环,较慢。
这里写一种较快的,dfs版的spfa。详见代码。
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define R register
using namespace std;
inline int read(){
R int x=;bool f=;
R char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return f?x:-x;
}
const int N=1e5+;
int T,n,m1,m2;
struct node{
int v,w,next;
}e[N<<];
int tot,head[N],dis[N];
bool can,flag[N];
void add(int x,int y,int z){
e[++tot].v=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
}
void spfa(int x){
flag[x]=;
for(int i=head[x];i;i=e[i].next){
int v=e[i].v,w=e[i].w;
if(dis[v]>dis[x]+w){
if(flag[v]||can){can=;break;}
dis[v]=dis[x]+w;
spfa(v);
}
}
flag[x]=;
}
void Cl(){
can=;tot=;
memset(dis,,sizeof dis);//注意不是inf
memset(head,,sizeof head);
memset(flag,,sizeof flag);
}
int main(){
T=read();
while(T--){
Cl();
n=read();m1=read();m2=read();
for(int i=,x,y,z;i<=m1;i++){
x=read();y=read();z=read();
add(x,y,z);
add(y,x,z);
}
for(int i=,x,y,z;i<=m2;i++){
x=read();y=read();z=read();
add(x,y,-z);
}
for(int i=;i<=n;i++){
spfa(i);
if(can) break;
}
puts(can?"YES":"NO");
}
return ;
}
POJ 3259 Wormholes (判负环)的更多相关文章
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- poj3259 Wormholes (判负环)【spfa】(模板)
<题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- 洛谷 P2850 [USACO06DEC]虫洞Wormholes 判负环
虫洞(wormhole) FJ 在农场上闲逛时,发现他的农场里有很多虫洞.虫洞是一条特殊的有向路径,当 FJ 从它的一头走到另一头后,他将被传送到过去的某个时刻.FJ 的每个农场包括 N(1<= ...
- poj 3259 Wormholes 判断负权值回路
Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...
- 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 - 3851-Wormholes(SPFA判负环)
A friend of yours, an inventor, has built a spaceship recently and wants to explore space with it. D ...
- poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)
/** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维 ...
随机推荐
- iOS之 opencv3.0.framework
本文章的目的是从源代码包中编译出opencv2.framework供IOS开发使用. 基本上是按照http://docs.opencv.org/3.0-beta/doc/tutorials/intro ...
- jar 命令 打包装class文件的文件夹
由于将spring源代码导入到eclipse后,缺少jar包, 所以从maven仓库中下载spring发布的spring-core jar包. 为了方便理解目录结构,使用tree命令: tr ...
- PHP判断访问者手机移动端还是PC端的函数,亲测好用
,用手机访问PC端WWW域名的时候,自动判断跳转到移动端,用电脑访问M域名手机网站的时候,自动跳转到PC端,我们团队在开发erdaicms二代旅游CMS网站管理系统的时候(http://www.erd ...
- 使用git的分支功能实现定制功能摘取与组合的想法
前言,这个想法应该是git比较通用的做法,只是我还没用过,所以把自己的想法记录在这里,督促自己以后按这个方式执行. 我们公司现在面临一个问题, 就是客户的定制需求很多,很杂,其中坑爹需求很多. 我还没 ...
- ArrayList
各种原因,前两年做C语言去了,现在重新做JAVA, 感觉自己基础很不扎实,要好好学习啦, 先从简单的开始~ 以下内容基于jdk1.7.0_79源码: 什么是ArrayList 可以简单的认为是一个动态 ...
- Web Service 的工作原理
Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...
- Spring Batch 批处理框架
<Spring Batch 批处理框架>基本信息作者: 刘相 出版社:电子工业出版社ISBN:9787121252419上架时间:2015-1-24出版日期:2015 年2月开本:16开页 ...
- javascript-简单工厂两种实现方式
简单工厂笔记 两种方式: 第一种:通过实例化对象创建 第二种:通过创建一个新对象然后包装增强其属性和功能来实现 差异性:前一种通过类创建的 对象,如果这些类继承同一个父类,他们父类原型上的方法是可以共 ...
- ASP.NET MVC 监控诊断、本地化和缓存
这篇博客主要是针对asp.net mvc项目的一些常用的东东做一个讲解,他们分别是监控诊断.本地化和缓存.虽然前两者跟asp.net mvc看上去好像是没什么关联. 但其实如果真正需要做asp.net ...
- proxool 连接池警告分析:appears to have started a thread named [HouseKeeper] but has failed to stop it
1. 问题:日志中出现下面的警告: 警告: The web application [ROOT] appears to have started a thread named [HouseKeeper ...