Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 37415   Accepted: 13764

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
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int m,n,cnt,p;
int head[1010],vis[1010],used[1010],dist[1010];
struct node
{
int u,v;
int val,next;
}edge[20010];
void add(int u,int v,int val)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].val=val;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int SPFA(int st)
{
queue<int>q;
memset(dist,INF,sizeof(dist));
memset(used,0,sizeof(used));
memset(vis,0,sizeof(vis));
vis[st]=1;
dist[st]=0;
q.push(st);//各种初始化
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;//进入循环一定要改成0,这样才能判断进入了多少次
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dist[v]>dist[u]+edge[i].val)
{
dist[v]=dist[u]+edge[i].val;
if(!vis[v])
{
vis[v]=1;
used[v]++;
if(used[v]>=m)//判断是否出现了负权边
return 1;
q.push(v);
}
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(head,-1,sizeof(head));
scanf("%d%d%d",&m,&n,&p);
int x,y,z;
while(n--)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
while(p--)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,-z);
add(y,x,INF);//虫洞是单向的,所以反向是无穷
}
if(SPFA(1)) printf("YES\n");
else printf("NO\n");
}
return 0;
}

poj 3259-- Wormholes(SPFA)的更多相关文章

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

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

  2. POJ 3259 Wormholes(SPFA判负环)

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

  3. poj 3259 Wormholes spfa算法

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

  4. POJ 3259 Wormholes SPFA算法题解

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

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

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

  6. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  7. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

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

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

  9. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  10. Poj(3259),SPFA,判负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. python--1、入门

    python的创始人为吉多·范罗苏姆(Guido van Rossum). python在2017年统计的所有语言排名中处于第四名,稳步上升状态. python应用领域: WEB开发(Django框架 ...

  2. OPPO R11 R11plus系列 解锁BootLoader ROOT Xposed 你的手机你做主

    首先准备好所有要使用到的文件 下载链接:https://share.weiyun.com/5WgQHtx 步骤1. 首先安装驱动 解压后执行 Install.bat 部分电脑需要禁用驱动程序签名才可以 ...

  3. ★Java语法(七)——————————循环语句

    package 自我总结; public class 循环语句 { public static void main(String[] args) { // while 循环 // 用法: // whi ...

  4. 【Oracle】服务器端监听配置

    一.静态监听 创建端口为1521的监听,静态注册,本机ip:192.168.10.2 [oracle@localhost ~]$ vi /u01/app/oracle/product/11.2.0/d ...

  5. I2C controller core之Bit controller(03)

    FPGA proven, AISC proven, I2C controller core from OpenCores http://opencores.org/project,i2c Bit-co ...

  6. JAVA面试题基础部分(二)

    10.使用 final 关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?使用 final 关键字修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内容还是可以改变的.例如,对于如下 ...

  7. dubbo之异步调用

    异步调用 基于 NIO 的非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小. 在 consumer.xml 中配置: <dubbo:reference ...

  8. Quartz+Topshelf 作业

    小记: 引用Quartz.Topshelf.Topshelf.Quartz 使用方法: http://www.cnblogs.com/mushroom/p/4952461.html http://ww ...

  9. HDU_5724_状态压缩的sg函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5724 题目大意:n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放 ...

  10. 【密码学】RSA加密 kotlin实现方法(支持任意字节长度)

    这个编辑器不支持kotlin,尴尬了···· 算了,就用Java来弄吧 val 定义常量 var 定义变量 具体kotlin的开发手册详见:http://www.runoob.com/kotlin/k ...