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. Jsp页面报错状态码含义

    原来,全部在HttpServletResponse接口的字段里 状态码 (),表示一个请求已经被接受处理,但还没有完成.  状态码 (),表明HTTP服务器从一个服务器收到了一个无效的响应,当其作为一 ...

  2. 使用NDK编译VTK

    VTK提供了对安卓的CMAKE编译支持,其介绍文件在源代码根目录下的 "/cmake/android.toolchain.cmake". 对Wndows的编译自持描述为: 注意:但 ...

  3. 用VS Code Debug Python

  4. java mongodb 使用MongoCollection,BasicDBObject 条件查询

    废话不说,上代码 //链接数据库 MongoClient mongoClient = new MongoClient( "172.26.xxx.xxx" , 27017 ); Mo ...

  5. commons.dbutils 的使用列子

    c0p3的导入请参考前文 https://www.cnblogs.com/appium/p/10183016.html JdbcUtils: package cn.itcast.jdbc; impor ...

  6. Ubuntu下解压(unzip)乱码的解决方法

    在Windows上压缩的文件,是以Windows系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 通过unzip -- ...

  7. Java 8 集合不完全一览

    JDK 8 List 名称 线程安全 数据结构 允许 null 默认初始容量 扩容策略 备注 ArrayList 不安全 数组 允许 10 1.5 * old LinkedList 不安全 双链表 允 ...

  8. Python学习【第1篇】:环境配置

    1. 下载安装包 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi    # 2.7安装包   https://www. ...

  9. STL源码分析之第一级配置器

    前言 上一节我们分析了空间配置器对new的配置, 而STL将空间配置器分为了两级, 第一级是直接调用malloc分配空间, 调用free释放空间, 第二级三就是建立一个内存池, 小于128字节的申请都 ...

  10. 系统和帮助-Linux基础知识

    iOS镜像: 硬盘分区:留出一些空间;实在不成,可安装完成以后,新增一块虚拟硬盘; 终端:terminal 用户界面: GUI:图形界面 GNome KDE CLI: bash,zsh,sh,csh, ...