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 ...
随机推荐
- [六]JFreeChart实践五之与Struts2整合
1.Action,返回Chart package com.java1234.chart.bar; import java.awt.Color; import org.jfree.chart.Chart ...
- Bootstrap-分页插件Paginator
Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...
- [React Native] Error Handling and ActivityIndicatorIOS
With React Native you use ActivityIndicatorIOS to show or hide a spinner. In this lesson we combine ...
- java中Map等对象转换为json
ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString( ...
- 在redhat6下配置yum源的使用
有好多朋友使用linux redhat版本是不是还在为rpm包的安装而烦恼,yum工具的使用无意是解决这一难题的好工具,他可以解决包安装中依赖问题,但是对于redhat版本的系统来说如果 ...
- SQL Server 2005下载安装
好久没弄过数据库了,打算重温一下,由于是新买的笔记本,所以今天下载安装了一下哈. 官网 http://www.microsoft.com/zh-cn/download/default.aspx 在官网 ...
- jdk安装 java运行编译(不含语法)
一.开发的准备 1.jdk的安装(window) (1)根据自己的电脑下载对应的jdk,并安装 (推荐安装在没有中文的目录中). 网站 http://www.oracle.com/technetwor ...
- 集合(Collection,set,list,map)
package cn.hncu.col.col; import java.util.ArrayList;import java.util.Collection;import java.util.Has ...
- (原创)如何在spannableString中使用自定义字体
最近在做车联网的产品,主打的是语音交互和导航功能,UI给的导航界面可真是够酷炫的.但麻烦的事情也来了,里面的一句话居然用到了三种字体.界面如图所示: 从图中可以看出 500m左前方行驶 居然使用了三种 ...
- jedis应用实例
最近将redis整合到项目中,将redis作为cache使用,未来进一步作为消息推送使用.我通过jedis和spring配置实现操作redis. spring配置 <!-- redis配置 -- ...