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. Eclipse里git提交冲突rejected – non-fast-forward

    Eclipse里commit代码,其实只是提交到本地仓库,需要push才会提交到远程的git仓库,这时是一个本地仓库到远程仓库的同步过程.Git是分布式的,每个人在本地仓库维护本地的自己的那一份代码, ...

  2. fuser命令

    fuser命令 http://blog.itpub.net/27573546/viewspace-765240/

  3. Oracle 2套rac集群指向单机多实例的复制搭建

    Oracle 2套rac集群指向单机多实例的复制搭建 由于环境限制,现在需要把2套rac集群通过dg复制指向远端的单机多实例上面. rac指向第一个实例的前面已经有文档 这里直接添加第二个实例的复制搭 ...

  4. python's twenty ninthday for me 模块和包

    模块 和 脚本的 区别:   如果一个py文件被导入了,就是一个模块. 如果这个py文件被直接执行,这个被直接执行的文件就是一个脚本. 模块:1,没有具体的调用过程.2,能对外提供功能. pyc文件: ...

  5. js(react.js) button click 事件无法触发

    今天遇到一个诡异的问题.button 上的点击事件触发不了. 找个几个小时,原因是 js 报错了. <Button type="primary" htmlType=" ...

  6. spring mvc学习 总体概览

      spring mvc 设计概览   springmvc处理http请求,主要是在web.xml中配置一个dispatcherservlet,然后由此进行拦截并处理请求返回相应,下面就针对源码大体记 ...

  7. <%@ page import=""%>的用法

    转自:https://blog.csdn.net/huihui870311/article/details/455642111 <jsp:directive.page import=" ...

  8. 基于C++11的线程池(threadpool),简洁且可以带任意多的参数

    咳咳.C++11 加入了线程库,从此告别了标准库不支持并发的历史.然而 c++ 对于多线程的支持还是比较低级,稍微高级一点的用法都需要自己去实现,譬如线程池.信号量等.线程池(thread pool) ...

  9. suse配置dhcp服务器

    Suse  dhcp服务器安装在安装系统时勾选 Suse dhcp 默认配置文件 /etc/dhcpd.conf Suse dhcp 启动程序 /etc/init.d/dhcpd restart 配置 ...

  10. POI技术

    1.excel左上角有绿色小图标说明单元格格式不匹配 2.模板中设置自动计算没效果,需要加上sheet.setForceFormulaRecalculation(true); FileInputStr ...