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..NM (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, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) 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.
题意:农夫约翰沉迷于守望屁股,现在他的农场里有一些双向道路和单向虫洞,穿过一条道路需要Ti的时间,进入虫洞则可以带你回到Ti时间之前,请问约翰是否可以在他出发之前回到他出发的农场(默认从1出发???),守望到他的屁股?
题解:很明显的一道spfa判负环
然后也没什么好说的了,WA到哭只因忘清空vector
代码如下:
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; vector< pair<int,int> > g[];
int d[],vis[],cnt[];
int ttt,n,m,w; int spfa()
{
memset(vis,,sizeof(vis));
memset(cnt,,sizeof(cnt));
d[]=;
queue<int> q;
q.push();
cnt[]++;
vis[]=;
while(!q.empty())
{
int x=q.front();
vis[x]=;
q.pop();
int sz=g[x].size();
for(int i=; i<sz; i++)
{
int y=g[x][i].first;
int w=g[x][i].second;
if(d[x]+w<d[y])
{
d[y]=d[x]+w;
if(!vis[y])
{
q.push(y);
vis[y]=;
cnt[y]++;
if(cnt[y]>n)
{
return ;
}
}
}
}
}
return ;
} int main()
{
scanf("%d",&ttt);
while(ttt--)
{
scanf("%d%d%d",&n,&m,&w);
for(int i=;i<=n;i++)
{
g[i].clear();
}
for(int i=; i<=m; i++)
{
int f,t,c;
scanf("%d%d%d",&f,&t,&c);
g[f].push_back(make_pair(t,c));
g[t].push_back(make_pair(f,c));
}
for(int i=; i<=w; i++)
{
int f,t,c;
scanf("%d%d%d",&f,&t,&c);
g[f].push_back(make_pair(t,-c));
}
for(int i=; i<=n; i++)
{
d[i]=inf;
}
int ans=spfa();
if(ans)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}
 
 
 
 
 

POJ3259 Wormholes(SPFA判断负环)的更多相关文章

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

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

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

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

  3. POJ3259 :Wormholes(SPFA判负环)

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

  4. POJ-3259 Wormholes(判断负环、模板)

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

  5. POJ3259 Wormholes —— spfa求负环

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

  6. POJ 3259 Wormholes【最短路/SPFA判断负环模板】

    农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...

  7. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

  8. spfa判断负环

    会了spfa这么长时间竟然不会判断负环,今天刚回.. [例题]poj3259 题目大意:当农场主 John 在开垦他的农场时,他发现了许多奇怪的昆虫洞.这些昆虫洞是单向的,并且可以把你从入口送到出口, ...

  9. spfa 判断负环 (转载)

    当然,对于Spfa判负环,实际上还有优化:就是把判断单个点的入队次数大于n改为:如果总的点入队次数大于所有点两倍 时有负环,或者单个点的入队次数大于sqrt(点数)有负环.这样时间复杂度就降了很多了. ...

随机推荐

  1. windows调试本地启动的tomcat

    我们一般都通过Eclipse进行tomcat的本地调试,但有些特殊情况下需要直接本地启动tomcat(进入tomcat的bin目录下执行startup.bat),此时我们可以利用类似远程调试模式的方式 ...

  2. 别人的dubbo学习笔记

    本文转载自:http://blog.csdn.net/tao_qq/article/details/49952229 学习dubbo,开始做一些笔记. 1> 启动dubbo-admin模块的时候 ...

  3. 关于bc 的scale .

    linux下的bc命令可以设置结果的位数,通过 scale. 比如: $ echo "scale=4; 1.2323293128 / 1.1" | bc -l1.1202 但是sc ...

  4. HTMLTestRunner生成报告 中文展示乱码的问题

  5. oracle 11g r2 使用rman进行迁移rac到rac

    源端服务器主机名 rac05 rac06公共IP地址(eth0) 10.15.8.15 10.15.8.16 虚拟IP地址(eth0) 10.15.8.17 10.15.8.18私有IP地址(eth1 ...

  6. oracle 11g r2 rac到单实例的dg

    oracle 11g r2 rac到单实例的dg 1 主备环境说明 rac环境--primary CentOS release 6.5 (Final)hostname rac1 rac2ip 10.* ...

  7. 获取Request.Form所有内容

    string wwww = "";        for (int i = 0; i < Request.Form.Count; i++)        {          ...

  8. getpass密码隐藏

    使用getpass模块对输入的字符进行隐藏输入 #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import getpass u ...

  9. babel-polyfill使用简介

    babel-polyfill介绍 简介 使用这个插件你可随心所欲的使用es6甚至更高版本的方法,这个插件自动转码 安装 这个插件必须在你的源码运行之前运行,所以必须安装成dependency npm ...

  10. MongoDB 学习笔记(一)—— 安装入门

    注:我的环境是win7 32位. 下载安装 http://www.mongodb.org/downloads 解压即可.这里我重命名“mongodb”,存放的目录为E:\mongodb. 新建数据文件 ...