BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description
John在他的农场中闲逛时发现了许多虫洞。虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前)。John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞。其中1<=N<=500,1<=M<=2500,1<=W<=200。 现在John想借助这些虫洞来回到过去(出发时刻之前),请你告诉他能办到吗。 John将向你提供F(1<=F<=5)个农场的地图。没有小路会耗费你超过10000秒的时间,当然也没有虫洞回帮你回到超过10000秒以前。
Input
* Line 1: 一个整数 F, 表示农场个数。
* Line 1 of each farm: 三个整数 N, M, W。
* Lines 2..M+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条用时T秒的小路。
* Lines M+2..M+W+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条可以使John到达T秒前的虫洞。
Output
* Lines 1..F: 如果John能在这个农场实现他的目标,输出"YES",否则输出"NO"。
题解:裸的不能再裸的 SPFA 判负环. 用 DFS 据说能更快一点
#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 10000
#define inf 10000000
using namespace std;
int hd[maxn],to[maxn<<1],nex[maxn<<1],val[maxn<<1];
int d[maxn],vis[maxn];
int edges,flag;
void add(int u,int v,int c)
{
nex[++edges]=hd[u], hd[u]=edges, to[edges]=v, val[edges]=c;
}
void spfa(int u)
{
int v;
vis[u]=1;
for(int i=hd[u];i;i=nex[i])
{
v=to[i];
if(d[u]+val[i]<d[v])
{
if(vis[v] || flag)
{
flag=1;
break;
}
d[v]=d[u]+val[i];
spfa(v);
}
}
vis[u]=0;
}
void re()
{
memset(hd,0,sizeof(hd));
memset(vis,0,sizeof(vis));
for(int i=1;i<maxn;++i) d[i]=inf;
edges=flag=0;
}
int main()
{
// setIO("input");
int F;
scanf("%d",&F);
while(F--)
{
re();
int n,m,w, a,b,c;
scanf("%d%d%d",&n,&m,&w);
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b, c),add(b,a,c);
}
for(int i=1;i<=w;++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,-c);
}
for(int i=1;i<=n;++i)
{
d[i]=0;
spfa(i);
if(flag) break;
}
if(flag==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环的更多相关文章
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MB 注意第一次加边是双向边第二次是单向边,并且每次询问前数 ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】
tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...
- 1715: [Usaco2006 Dec]Wormholes 虫洞
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 501 Solved: 278[Sub ...
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- BZOJ1715: [Usaco2006 Dec]Wormholes 虫洞
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 475 Solved: 263[Sub ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
随机推荐
- POJ 3252 Round Numbers 组合数学
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13381 Accepted: 5208 Description The ...
- Q - Period II
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...
- PHP array_intersect_ukey()
定义和用法 array_intersect_ukey() 函数用回调函数比较键名来计算数组的交集. array_intersect_ukey() 返回一个数组,该数组包含了所有出现在 array1 中 ...
- SharePoint 2013 改动表单认证登录页面
前 言 之前的博客我们介绍了怎样为SharePoint配置表单登陆,可是.登陆页面是丑.非常丑.非常丑.特别非常丑! 我们如今就介绍一下怎样定制SharePoint表单登陆页面! SharePoint ...
- 【POJ 1475】 Pushing Boxes
[题目链接] http://poj.org/problem?id=1475 [算法] 双重BFS [代码] #include <algorithm> #include <bitset ...
- 【转】iPhone获取状态栏和导航栏尺寸(宽度和高度)
原文网址:http://blog.csdn.net/chadeltu/article/details/42708605 iPhone开发当中,有时需要获取状态栏和导航栏高度.宽度信息,方便布局其他控件 ...
- 洛谷 P3959 NOIP2017 宝藏 —— 状压搜索
题目:https://www.luogu.org/problemnew/show/P3959 搜索: 不是记忆化,而是剪枝: 邻接矩阵存边即可,因为显然没有那么多边. 代码如下: #include&l ...
- Principal Component Analysis ---- PRML读书笔记
To summarize, principal component analysis involves evaluating the mean x and the covariance matrix ...
- aspectC++常用命令
常用命令:1.ag++ main.cc //在工程目录下产生编译后的exe2.ag++ main.cc --weave_only //产生.acc 纯c++文件3.ag++ main.cc --gen ...
- E20170816-mk
deque 即双端队列.是一种具有队列和栈的性质的数据结构. revert vi. 恢复; 重提; 回到…上; <律>归还; n. 归属; 恢复原来信仰的人; Indicator ...