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判负环)的更多相关文章

  1. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  2. Wormholes POJ 3259(SPFA判负环)

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

  3. POJ 3259 Wormholes (判负环)

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

  4. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  5. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  6. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  7. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  8. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  9. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

随机推荐

  1. 各种sensor名称统计

    gyroscopes 陀螺仪accelerometers 加速度计magnetometers 磁力计barometric pressure 气压remote pressure sensing 远程压力 ...

  2. UEditor 使用过程中问题

    UEditor  百度的文本编辑器   具体详细信息请去百度文本编辑器官网,这里不多说了. 我使用的是 UBuilder 版本的NET版本 ,我只是需要图片上传功能,所以就做了最小化的定制.(可以在官 ...

  3. 算法导论学习-heapsort

    heap的定义:如果数组a[1,....n]满足:a[i]>a[2*i] && a[i]>a[2*i+1],1<=i<=n/2,那么就是一个heap,而且是ma ...

  4. DevExpress控件XtraGrid的Master-Detail中DetailViewCaption显示问题

    今天遇到XtraGrid身档抬头显示不对问题,查找了很多资料后未找到解决办法,后浏览至此文作者发现解决问题的方法就是:DataSet中的RelactionName跟DetailGridview的Lea ...

  5. Redis指令文档

    连接控制QUIT 关闭连接AUTH (仅限启用时)简单的密码验证 适合全体类型的命令EXISTS key 判断一个键是否存在;存在返回 1;否则返回0;DEL key 删除某个key,或是一系列key ...

  6. ios:Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

    - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)laun ...

  7. 如何将中国知网CNKI中的文献导入EndNote X6

    如何将中国知网CNKI中的文献导入EndNote X6 下面给出具体步骤: 1.在CNKI中检索目标文献,如检索<基于Qt的三维可视化技术研究> 我喜欢在CNKI(http://www.c ...

  8. JDBC与SQL SERVER各个版本的连接方法

    转至:blog.csdn.net/ying5420/article/details/4488246 1.SQL SERVER 2000 JDBC驱动程序:msbase.jar.mssqlserver. ...

  9. JBoss 目录结构

    安装JBoss 会创建下列目录结构: 目录 描述  bin 启动和关闭JBoss 的脚本  client 客户端与JBoss 通信所需的Java 库(JARs)  docs 配置的样本文件(数据库配置 ...

  10. String的点点滴滴

    一.String 的 equals()到底比较的是什么?equals() 与 == 的区别? 当使用关系运算符==比较两个对象时,是比较两个对象使用的内存地址和内容是否相同,如果两个对象使用的是同一个 ...