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双端队列优化: 维 ...
随机推荐
- java的反射机制
一.java的反射机制浅谈 最近研究java研究得很给力,主要以看博文为学习方式.以下是我对java的反射机制所产生的一些感悟,希望各位童鞋看到失误之处不吝指出.受到各位指教之处,如若让小生好好感动, ...
- 转:JS获取浏览器高度和宽度
发现一篇好文章,汇总到自己的网站上. IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> ...
- MicroStation VBA 可视化界面
第十章 可视界面 Private Sub UserForm_Initialize() Dim ViewCen As Point3d Dim MyView As View For Each MyView ...
- Amoeba for MySQL---分布式数据库Proxy解决方案
Amoeba是什么? Amoeba(变形虫)项目,致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy ...
- 【转发】NPAPI开发详解,Windows版
NPAPI开发详解,Windows版 9 jiaofeng601, +479 9人支持,来自Meteor.猪爪.hanyuxinting更多 .是非黑白 .Yuan Xulei.hyolin.Andy ...
- Java查询大文本
但JAVA本身缺少相应的类库,需要硬编码才能实现结构化文件计算,代码复杂且可读性差,难以实现高效的并行处理. 使用免费的集算器可以弥补这一不足.集算器封装了丰富的结构化文件读写和游标计算函数,书写简单 ...
- x01.os.5: DOS 功能调用
DOS 功能调用(INT 21)-------------------------------AH = 0-2E 适用 DOS 1.0 以上版本AH = 2F-57 适用 DOS 2.0 以上版本AH ...
- Install pyodbc in OpenSUSE
Install pyodbc in OpenSUSE: Install unixODBC-2.3.2 ./configure --prefix=/usr/local/unixODBC --enable ...
- [转]simple sample to create and use widget for nopcommerce
本文转自:http://badpaybad.info/simple-sample-to-create-and-use-widget-for-nopcommerce Here is very simpl ...
- 百度CDN 网站SSL 配置
百度CDN SSL配置步骤 一般从SSL提供商购买到的证书是CRT二进制格式的. 1. 将 CRT 导入到IIS中, 然后从IIS中导出为PFX格式 2. 下载openssl,执行下面命令 提取用户证 ...