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. (转)Apache转发配置

    本文转载自:http://blog.csdn.net/leshjmail/article/details/6163581 安装文件 1.httpd-2.2.15-win32-x86-no_ssl.ms ...

  2. PHP面向对象深入研究之【继承】,减少代码重复

    继承 先看两个类 <?php class CdProduct { public $playLength; // 播放时间 public $title; public $producerMainN ...

  3. CP干货:手机游戏上线前需要准备什么

    转自:http://www.gamelook.com.cn/2015/09/229002 游戏研发完成后游戏该怎样推广?如何找渠道?推广时需要注意什么?下面给大家介绍一下具体流程,可能每个公司的上线流 ...

  4. 使用QuartZ.net来做定时计划任务 ; 值不能为 null。 参数名: assemblyString

    1. 当异常的时候, 发现需要的类名, 没有取到, 然后就发生异常了 2. 分析: 业务层调用数据层, 数据层去掉配置的时候, 发现配置文件中根本就没有配置, 这个时候使用反射来取, 肯定是拿不到的, ...

  5. 解决webpack环境变量NODE_ENV跨平台兼容性问题

    为什么要用cross-env插件 学过webpack的人都知道,webpack可以使用NODE_ENV=development或者NODE_ENV=production这样的方式来设置全局变量 这样的 ...

  6. http协议请求响应内容示例

    POST http://www.cytxl.com.cn/api/common/login.php?XDEBUG_SESSION_START=netbeans-xdebug HTTP/1.1 Host ...

  7. day3心得

    1. 集合 主要作用: 去重 关系测试, 交集\差集\并集\反向(对称)差集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >>&g ...

  8. leetcode893

    class Solution { public: int numSpecialEquivGroups(vector<string>& A) { set<string> ...

  9. 搭建Easyui环境在Myeclipse或Eclipse中

    转自:https://www.cnblogs.com/henuyuxiang/p/4283018.html 1.下载Easyui.网址:http://www.jeasyui.com/download/ ...

  10. 在ubuntu中编译内核是用make&nbsp;…

    执行过程如下: root@zyx-VirtualBox:~# cd /opt/EmbedSky/ root@zyx-VirtualBox:/opt/EmbedSky# cd linux-2.6.30. ...