Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36852   Accepted: 13502

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

分析:

英语不好。就不在翻译了。意思就是求最短路。假设最短路中有负环,输出yes,没有输出no。

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define maxn 10010
using namespace std; int N,M,W;
int pnum;
int dis[maxn];
int vis[maxn];
int head[maxn];
int used[maxn];
bool cnt;
struct node{
int from;
int to;
int val;
int next;
};
node pp[2*maxn]; void addpp(int u,int v,int w)
{
node E ={u,v,w,head[u]};
pp[pnum]=E;
head[u]=pnum++;
} void init()
{
int a,b,c;
while(M--)
{
scanf("%d%d%d",&a,&b,&c);
addpp(a,b,c);
addpp(b,a,c);
}
} void gmap()
{
int a,b,c;
while(W--)
{
scanf("%d%d%d",&a,&b,&c);
addpp(a,b,-c);
}
} void SPFA(int s)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
dis[s]=0;
vis[s]=1;
used[s]++;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=pp[i].next)
{
int v=pp[i].to;
if(dis[v]>dis[u]+pp[i].val)
{
dis[v]=dis[u]+pp[i].val;
if(!vis[v])
{ vis[v]=1;
q.push(v);
used[v]++;
if(used[v]>N)
{
cnt=true;
return;
}
}
}
}
} } int main(){
int T;
scanf("%d",&T);
while(T--)
{
pnum=0;
cnt=false;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&N,&M,&W);
init();
gmap();
SPFA(1);
if(cnt)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

poj 3259 Wormholes 【SPFA&amp;&amp;推断负环】的更多相关文章

  1. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  2. POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 49962   Accepted: 18421 Descr ...

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

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

  4. POJ 3259 Wormholes SPFA算法题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  5. uva558 Wormholes SPFA 求是否存在负环

    J - Wormholes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  6. POJ 3259 Wormholes(SPFA判负环)

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

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

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

  8. [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Descr ...

  9. poj 3259 Wormholes spfa算法

    点击打开链接 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25582   Accepted: 9186 ...

随机推荐

  1. How to read a paper efficiently

    How to read a paper efficiently Structure of a Journal a Journal Article Title Keywords Abstract Int ...

  2. 如何用ajax写分页查询(以留言信息为例)-----2017-05-17

    要写分页,首先你得清楚,一页你想显示多少条信息?如何计算总共显示的页数? 先说一下思路: (1)从数据库读取数据,以chenai表为例,读取所有留言信息.并能够实现输入发送者,可以查询该发送者的留言总 ...

  3. 基于Apache Thrift的公路涵洞数据交互实现原理

    基于Apache Thrift的公路涵洞数据交互实现原理 Apache Thrift简介 Apache Thrift(以下简称为“Thrift”) 是 Facebook 实现的一种高效的.支持多种编程 ...

  4. 二次排序问题(分别使用Hadoop和Spark实现)

    不多说,直接上干货! 这篇博客里的算法部分的内容来自<数据算法:Hadoop/Spark大数据处理技巧>一书,不过书中的代码虽然思路正确,但是代码不完整,并且只有java部分的编程,我在它 ...

  5. HBase编程 API入门系列之delete.deleteColumn和delete.deleteColumns区别(客户端而言)(4)

    心得,写在前面的话,也许,中间会要多次执行,连接超时,多试试就好了. delete.deleteColumn和delete.deleteColumns区别是: deleteColumn是删除某一个列簇 ...

  6. Goldengate升级之目标端(replicat端)升级

    转自红黑联盟Goldengate升级之目标端(replicat端升级 要升级replicat端的原因为:目标端OGG软件版本与源端OGG软件版本不同,在实际生产应用中,经常发现replicat端事务丢 ...

  7. (转)shiro权限框架详解02-权限理论介绍

    http://blog.csdn.net/facekbook/article/details/54893042 权限管理解决方案 本文主要介绍权限管理的解决方法: 粗颗粒度和细颗粒度 基于url拦截 ...

  8. sqlserver 分组 group by

    select 名称, COUNT(名称) as 数量之和from 信息group by all 名称

  9. day06-08面向对象的三大特性

    一.继承 1.1什么是继承?继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类python中类的继承分为:单继承和多继承 cl ...

  10. HDU 4704 Sum( 费马小定理 + 快速幂 )

    链接:传送门 题意:求 N 的拆分数 思路: 吐嘈:求一个数 N 的拆分方案数,但是这个拆分方案十分 cd ,例如:4 = 4 , 4 = 1 + 3 , 4 = 3 + 1 , 4 = 2 + 2 ...