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. linux(6)

    第十五单元 软件包的管理 [本节内容]1. 使用RPM安装及移除软件(详见linux系统管理P374)1) 掌握RPM的定义:RPM就是Red Hat Package Manger(红帽软件包管理工具 ...

  2. 关于Python导入其他目录中的类

    在需要导入的某个类的目录中,添加一个__init__.py的文件,

  3. 2016女生赛 HDU 5710 Digit-Sum(数学,思维题)

    Digit-Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...

  4. Java-Maven-Runoob:Maven教程

    ylbtech-Java-Maven-Runoob:Maven教程 1.返回顶部 1. Maven 教程 Maven 翻译为"专家"."内行",是 Apache ...

  5. mysql replication /mysql 主从复制原理

    一下内容均是根据leader的培训分享整理而成 ************************************我是分割线*********************************** ...

  6. angular 本地存储

    localStorage四种方法: localStorage.getItem(key):获取指定key本地存储的值localStorage.setItem(key,value):将value存储到ke ...

  7. 二 mysql容量规划,性能测试

    何为基线- 当前运行状态记录.快照- 用于和未来的状态进行对比- 未来时刻产生关键事件后的新状态,作为下一个基线基线数据收集,关注哪些要点- 系统负载- MySQL运行状态- 相应的业务指标1.系统& ...

  8. Java学习之Mysql结构优化

    背景:业务发展初期为了便于快速迭代,很多应用都采用集中式的架构,随着业务规模的扩展,系统变得越来越复杂,访问量越来越大,不得不进一步扩展系统的吞吐能力. 优化1.主从集群:通过数据库的复制策略,可以将 ...

  9. java 调用系统外部的某个程序

    有时候我们java 调用系统外部的某个程序 可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回 ...

  10. hardentools

     Hardentools是一组简单的实用程序,旨在禁用操作系统(Microsoft Windows,现在)以及主要的消费者应用程序公开的许多“功能”.这些通常为企业客户所设想的功能,对于普通用户来说通 ...