Poj 3259 Wormholes(spfa判负环)
Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 42366 Accepted: 15560
传送门
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: A single integer, F. F farm descriptions follow.
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
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
/有道翻译2.0/
虫洞
时间限制: 2000毫秒 内存限制: 65536 k
总提交: 42366 接受: 15560
描述
在探索他的许多农场,农场主约翰发现了一些惊人的虫洞。 虫洞非常特殊,因为它是一种单向路径送你到目的地之前进入虫洞! 每个FJ农场组成的 N (1≤ N ≤500)字段方便编号1 . . N , 米 (1≤ 米 ≤2500)路径,和 W (1≤ W ≤200)虫洞。
FJ穿越时光的狂热粉丝,他想做以下几点:开始在一些领域,通过一些路径和虫洞旅行,回到起始时间他最初的离开。 也许他将能够满足自己:)。
帮助FJ找出是否这是可能的,他将为你提供完整的地图 F (1≤ F ≤5)他的农场。 没有路径将超过10000秒,没有虫洞旅行可以带回FJ时间超过10000秒。
输入
1号线:一个整数, F 。 F 农场的描述。
每个农场的第1行:三个空格分隔的整数分别为: N , 米 , W
行2 . . 米 每个农场的+ 1:三个空格分隔的数字( 年代 , E , T )描述,分别为:之间的双向道路 年代 和 E 这需要 T 秒遍历。 两个字段可能被多个连接路径。
行 米 + 2 . . 米 + W 每个农场的+ 1:三个空格分隔的数字( 年代 , E , T )描述,分别为:一个路径的一种方式 年代 来 E 这也将旅行回来 T 秒。
输出
行1 . . F :对于每个农场,输出“YES”如果FJ能实现他的目标,否则输出“不”(不包括引号)。
样例输入
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
样例输出
NO
YES
提示
对于农场1,FJ不能穿越时间。
对于农场2,FJ可以穿越时间的周期1 - > 2 - > 3 - > 1,到达之前回到他的起始位置1秒。 他可以从任何地方开始循环来完成这项工作。
源
USACO 2006 12月黄金
/*
邻接矩阵+spfa判负环.
正权值建图双向.
负权值建图单向.
然后定理:若图中一点入队超过n次就出现了负环(bfs).
若路径中多次出现一点就出现了负环(dfs).
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define MAXN 25001
using namespace std;
int s[MAXN],head[MAXN],n,m,tot,cut,dis[MAXN],k;
bool b[MAXN];
struct data
{
int v;
int next;
int z;
}e[MAXN];
void add(int u,int v,int z)
{
e[cut].v=v;
e[cut].next=head[u];
e[cut].z=z;
head[u]=cut++;
}
bool spfa()
{
queue<int>q;
q.push(1);dis[1]=0;b[1]=true;s[1]++;
while(!q.empty())
{
int u=q.front();q.pop();b[u]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(dis[v]>dis[u]+e[i].z)
{
dis[v]=dis[u]+e[i].z;
if(!b[v])
{
b[v]=true;
s[v]++;
if(s[v]>=n) return 1;
q.push(v);
}
}
}
}
return 0;
}
void init()
{
cut=0;
memset(dis,0x7f,sizeof(dis));
memset(b,0,sizeof(b));
memset(head,-1,sizeof(head));
memset(s,0,sizeof(s));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
int x,y,z;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
for(int i=1;i<=k;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,-z);
}
if(spfa())
{
printf("YES\n");
}
else printf("NO\n");
}
return 0;
}
Poj 3259 Wormholes(spfa判负环)的更多相关文章
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- POJ 3259 Wormholes( bellmanFord判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36425 Accepted: 13320 Descr ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
随机推荐
- HW4.7
public class Solution { public static void main(String[] args) { double rate = 0.05; double balance ...
- 发布ASP(非.Net)网站
1.安装IIS 2.设置网址.端口 3.设置文档(默认访问的文档,比如index.asp,index.htm等) 4.双击asp - 展开行为 - 启用父路径:true - 允许访问父目录 5.应用程 ...
- php排序之冒泡排序
冒泡排序比较简单.作为很多公司面试笔试题常常出现,要求手写该排序算法.双层循环,不断的与后面的比较,如果大于后面的,调换两者顺序即可. 演示效果如图: 代码如下: <?php function ...
- oracl使用DataBase Configuration Assistant创建、删除数据库
原文:oracl使用DataBase Configuration Assistant创建.删除数据库 可以使用DataBase Configuration Assistant来创建一个心得数据库.Da ...
- Jquery UI 组合树 - ComboTree 集成Wabacus4.1 代码剖析
Jquery UI 1.3 (组合树 - ComboTree ) 集成Wabacus4.1 集成Spring 代码剖析 使用时,请下载需要Jquery ui包进行配置 combotree.js 的代码 ...
- easyui valid
/** * 包含easyui的扩展和常用的方法 * * @author * * @version 20120806 */ var wjc = $.extend({}, wjc);/* 定义全局对象,类 ...
- Android UI开发第三十三篇——Navigation Drawer For Android API 7
Creating a Navigation Drawer中使用的Navigation Drawer的android:minSdkVersion="14",现在Android API ...
- java class 文件解析
参考下面两个文章对一个class文件进行解析: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.6 htt ...
- Android自定义View之ProgressBar出场记
关于自定义View,我们前面已经有三篇文章在介绍了,如果筒子们还没阅读,建议先看一下,分别是android自定义View之钟表诞生记.android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检 ...
- VB学习笔记
stack segment stack 'stack' dw dup() ;此处输入堆栈段代码 stack ends data segment ;IBUF OBUF 看成是内存的地址,IBUF+1和I ...