Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 46123   Accepted: 17033

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..NM (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: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM,
and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET)
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 (SET)
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

Hint

For farm 1, FJ cannot travel back in time. 
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

USACO 2006 December Gold

题目大意:
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 (判负环)的更多相关文章

  1. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  2. poj3259 Wormholes (判负环)【spfa】(模板)

    <题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...

  3. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

  4. poj 3259 (Bellman_Ford判断负环)

    题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...

  5. 洛谷 P2850 [USACO06DEC]虫洞Wormholes 判负环

    虫洞(wormhole) FJ 在农场上闲逛时,发现他的农场里有很多虫洞.虫洞是一条特殊的有向路径,当 FJ 从它的一头走到另一头后,他将被传送到过去的某个时刻.FJ 的每个农场包括 N(1<= ...

  6. poj 3259 Wormholes 判断负权值回路

    Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  7. POJ 3259 Wormholes Bellman_ford负权回路

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  8. POJ 3259 Wormholes(负权环路)

    题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...

  9. POJ - 3851-Wormholes(SPFA判负环)

    A friend of yours, an inventor, has built a spaceship recently and wants to explore space with it. D ...

  10. poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)

    /** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维 ...

随机推荐

  1. objective-c系列-动态类型和动态绑定

    /* 静态类型: 变量的类型在编译之时就被确定下来. 动态类型: 对象的类型由对象的内存里的某个结构数据来决定它是什么类型, 而不是在编译之时就被确定下来的数据类型. 对象的类型只有在运行时才知道. ...

  2. 干货之UIButton的title和image自定义布局

    当需要实现一个自定义布局图片和标题的按钮时候,不知道有多少少年直接布局了UIButton,亦或是自定义一个UIView,然后以空白UIButton.UILabel.UIImageVew作为subVie ...

  3. iOS中View生命周期与布局

    生命周期 说到view的生命周期一般都是指视图控制器的view生命周期. view的创建:loadView 视图控制器(UIViewController)及其子类,无论是手写代码还是storyboar ...

  4. FMDB的简单使用

    1.什么是FMDB? FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API. 无论项目中使用 ARC 还是 MRC,对 FMDB 都没有任何影响,FMDB ...

  5. 最新Internet Download Manager (IDMan) 6.25 Build 20 32位 64位注册破解补丁

    0x00 IDMan介绍 Internet Download Manager提升你的下载速度最多达5倍,安排下载时程,或续传一半的软件.Internet Download Manager的续传功能可以 ...

  6. servlet 学习(一)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  7. Java并发之工具类 ForkJoin 任务分解

    Fork/Join框架的介绍 第一步分割任务.首先我们需要有一个fork类来把大任务分割成子任务,有可能子任务还是很大,所以还需要不停的分割,直到分割出的子任务足够小. 第二步执行任务并合并结果.分割 ...

  8. ORA-01858: 在要求输入数字处找到非数字字符

    数据库   date  字段问题  insert into  WK_RE_LE  (DACL_FILE_ID,DACL_GROUP_ID,BDCDYH,DACL_LENGTH,ISVALID,DACL ...

  9. Java api 入门教程 之 JAVA的IO处理

    IO是输入和输出的简称,在实际的使用时,输入和输出是有方向的.就像现实中两个人之间借钱一样,例如A借钱给B,相对于A来说是借出,而相对于B来说则是借入.所以在程序中提到输入和输出时,也需要区分清楚是相 ...

  10. 透过byte数组简单分析Java序列化、Kryo、ProtoBuf序列化

    序列化在高性能网络编程.分布式系统开发中是举足轻重的之前有用过Java序列化.ProtocolBuffer等,在这篇文章这里中简单分析序列化后的byte数组观察各种序列化的差异与性能,这里主要分析Ja ...