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 ...
随机推荐
- Esper系列(一)初探
Esper介绍 Esper是一个Java开发并且开源的轻量级和可扩展的事件流处理和复合事件处理引擎,并提供了定制的事件处理语言(EPL). 应用场景 某个用户在请求登录服务时,n秒内连续m次未登录成功 ...
- HW2.4
import java.util.Scanner; public class Solution { public static void main(String[] args) { final dou ...
- POJ1873 - Balance(01背包)
题目大意 现有一个天平,它有C个挂钩和G个砝码,问有多少种方法可以使得天平平衡(砝码必须用完) 题解 其实就是让背包容量为0的方法有多少种方法,但是这样的话背包容量会出现负数,所以可以平移一下,背包容 ...
- poj 1438--One-way Traffic(边的双连通)
给定一个图,并给定边,a b c(c==1||c==2) 表示ab之间有c条边 求把尽可能多的有向边定向变成强联通图. 先把图当做无向图,加边时记录是否有边,dfs的时候不要把本没有的边用到!因为这个 ...
- NSNumber和Int之间的转换
int 转 NSNumber: [NSNumber numberWithInt:(int)]; NSNumber 转 int [(NSNumber) intValue]; 其他数据类型类似 有 ...
- 检查REDO日志相关信息并生成HTML文件的脚本
生成HTML格式的文件 内容有: 检查数据库版本.REDO日志组情况, 最近20次日志切换频率检查--日志间的归档时间间隔, 这对查看数据库的IO繁忙时段 统计指定日期当天每小时的归档日志产生量--日 ...
- html图片滚动效果
分享一个手动控制图片左右滚动的代码 先说html部分,建立一个层,写出他的样式,层中在建立一个小一点的层用来存放需要滚动的图片,小层两边再建两个小层用来存放控制图片左右滚动的按钮.代码如下: 样式表: ...
- linq to sql 博客集锦
Linq to sql 比较全面的学习博客 http://www.cnblogs.com/aehyok/tag/Linq%20To%20Sql/ 使用LINQ TO SQL基于Respository ...
- git 设置
系统乱码 项目中的编码统一设置为UTF-8编码. 设置系统的语言设置为 zh_UTF-8,把 export LANG=zh_CN.UTF-8 保存到~/.profile文件里. $ env|grep ...
- VC中获取窗体句柄的各种方法
AfxGetMainWnd AfxGetMainWnd获取自身窗体句柄 HWND hWnd = AfxGetMainWnd()->m_hWnd; GetTopWindow 函数功能:该函数检查与 ...