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.
解题思路:本题可用floyd或者spfa算法来判断是否有负环。题意:有f个农场,每个农场有n块地,其间有m条路,w条虫洞,穿过虫洞之后相应的时间会倒退t秒,问是否可以从某块地出发到达另一块地之后通过虫洞逆流时间回到当初出发的那个时间点。怎么建图呢?建m条双向边,边权为正数,建k条单向边,边权为负数(时光逆流)。显然,我们只需判断整个图中是否存在一个权值之和为负数的回路即可。因为若存在了负环,沿着回路走,时间就会不断地减少,那么FJ就肯定能遇到当初那个时刻出发的自己。
spfa核心思想:构建一个队列,将松弛成功且不在队列的点加入队列中,同时将顶点v的入队次数加1并标记为true,表示该顶点在当前队列中,避免后面的顶点重复入队造成更新时间上的浪费,然后每出队一个点就将其标记为false,因为出队的顶点可能会对前面已更新的点到源点的距离有影响并可能再次入队,这样直到队列为空。如果队列中的某个顶点入队次数超过n次,就说明存在一个负环。为什么是大于n呢?如果是大于n-1,那么任意的单节点的图会被判定为存在负环,综合考虑取>n。
AC代码(141ms):
 #include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
const int maxn=;
int T,x,y,z,n,m,k,u,v,w,cnt[maxn],dis[maxn];vector<int> v1[maxn],v2[maxn];bool vis[maxn];queue<int> que;
bool spfa(int s){
while(!que.empty())que.pop();
memset(vis,false,sizeof(vis));
que.push(s),vis[s]=true,cnt[s]++,dis[s]=;//注意:自己到本身的时间为dis[s]=0,同时累计每个顶点的入队次数,用于判负环
while(!que.empty()){
u=que.front(),que.pop(),vis[u]=false;
for(size_t j=;j<v1[u].size();++j){
v=v1[u][j],w=v2[u][j];
if(dis[u]+w<dis[v]){//松弛
dis[v]=dis[u]+w;
if(!vis[v]){
que.push(v),vis[v]=true;
if(++cnt[v]>n)return true;//如果第n+1次仍然更新,则存在负圈
}
}
}
}
return false;
}
int main(){
while(~scanf("%d",&T)){
while(T--){
scanf("%d%d%d",&n,&m,&k);memset(cnt,,sizeof(cnt));
for(int i=;i<=n;++i)v1[i].clear(),v2[i].clear();
for(int i=;i<=n;++i)dis[i]=2e9;
while(m--){
scanf("%d%d%d",&x,&y,&z);//建双向边
v1[x].push_back(y),v1[y].push_back(x);
v2[x].push_back(z),v2[y].push_back(z);
}
while(k--){
scanf("%d%d%d",&x,&y,&z);//建单向边,权值为负数,表示时间倒流
v1[x].push_back(y);
v2[x].push_back(-z);
}
if(spfa())puts("YES");
else puts("NO");
}
}
return ;
}

解题报告:poj 3259 Wormholes(入门spfa判断负环)的更多相关文章

  1. poj 3259 Wormholes【spfa判断负环】

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36729   Accepted: 13444 Descr ...

  2. (简单) POJ 3259 Wormholes,SPFA判断负环。

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

  3. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  4. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. poj 3259 Wormholes(bellman-ford判断负环)

    题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判 ...

  6. POJ 3259 Wormholes【Bellman_ford判断负环】

    题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...

  7. bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环

    1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec  Memory Limit: 64 MB 注意第一次加边是双向边第二次是单向边,并且每次询问前数 ...

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

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

  9. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

随机推荐

  1. cmd下并行执行appium +maven+Testng test

    1: first: open two command wins to start appium: appium -p 4725 -bp 4726 -U EP7333W0UR appium -p 472 ...

  2. 有遍历struct中字段信息的函数或方法

    例:struct a{int a;char b[10];double c;}; 在程序中只知道一个结构 a 的指针, 有没有函数能通过结构的名字 和 指向结构的指针 随次得到 结构中的变量类型 和 变 ...

  3. SVN回滚机制

    引子 工作中遇到一个新同事提交代码时不知怎么的出现了大面积的代码覆盖,由于对SVN也不是特别了解,就看着别人处理问题,自己也验证性的实践了一下,总结一下. 总结 svn每一次提交成功,都会有一个`编号 ...

  4. 控制cms:CMSDropDownList的width

    查找了一下kentico内部使用相关控件的代码,发现有2种方式,可以达到效果. 在我们自己的css class定义的地方,在class前面加上.cms-bootstrap .cms-bootstrap ...

  5. MYSQL初级学习笔记一:MYSQL常用命令和数据库操作(DDL)!(视频序号:初级_3,4)

    知识点一:MYSQL常用命令(3) 登入方法:一,mysql –u 账号 –p 密码 退出方法:一,EXIT,QUIT 修改MYSQL命令提示符: 连接上客户机之后,通常使用prompt命令修改: 连 ...

  6. BM算法模式匹配——字符串与KMP比较

    下面是代码:BM是什么参考阮一峰老师的讲解  点击打开链接 #include<iostream> #include<algorithm> #include<string. ...

  7. oracle long 转varchar2

    函数: /* 其中in_rowid为行id,in_owner为数据库登陆的帐号名,in_table_name为数据库表名,in_column为数据库对应long类型的表字段名称 */ CREATE O ...

  8. python中的异常处理try/except/finally/raise

    异常发生在程序执行的过程中,如果python无法正常处理程序就会发生异常,导致整个程序终止执行,python中使用try/except语句可以捕获异常. try/except 异常的种类有很多,在不确 ...

  9. WebDev.WebServer40.EXE命令使用

    "C:\Program Files (x86)\Common Files\microsoft shared\DevServer\10.0\WebDev.WebServer40.EXE&quo ...

  10. 微信小程序服务类目大坑:特殊行业服务类目所需资质材料

    作为一个技术开发人员,遇到特殊行业服务类目所需资质材料,只能叫苦连天了,妈的,这个不是技术可以解决的问题,如果技术可以解决的问题都不是问题. 百牛信息技术bainiu.ltd整理发布于博客园 特殊行业 ...