解题关键:模板保存。

判负环不需要memset dis数组,因为已经更新过得d数组一定小于0,如果当前点可以更新d,说明d更小,有可能继续扩大负环,所以继续更新;如果比d[v]大,则不可能继续更新负环,所以直接终止。

 有向图只扫一个点貌似不可以。。。bfs_spfa的时候一定注意,但dfs_spfa一定可以。

dfs过不了这道题,因为必须经过1这个点,或许d不置0可以,但会超时?

有向图可以使用拓扑排序找环。

1、dfs

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
const int inf=0x3f3f3f3f;
const int maxm=;
const int maxn=;
int head[maxn],tot,n,m;
struct edge{
int to;
int w;
int nxt;
}e[maxm];
void add_edge(int u,int v,int w){
e[tot].w=w;
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
bool vis[maxn];
int d[maxn];
//
bool dfs_spfa(int u){
vis[u]=;
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
if(vis[v]||dfs_spfa(v)) return ;
}
}
vis[u]=;
return ;
}
int main(){
int a,b,c,T;
scanf("%d",&T);
while(T--){
tot=;
memset(head,-,sizeof head);
memset(vis,,sizeof vis);
memset(d,,sizeof d);
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){//注意为双向边
scanf("%d%d%d",&a,&b,&c);
if(c<) add_edge(a,b,c);
else add_edge(a,b,c),add_edge(b,a,c);
}
bool flag=false;
for(int i=;i<=n;i++){
if(dfs_spfa(i)){
flag=true;
break;
}
}
if(flag) puts("YE5");
else puts("N0");
}
return ;
}

2、bfs_spfa

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxm=;
const int maxn=;
int head[maxn],tot,n,m,cnt[maxn],w;
struct edge{
int to;
int w;
int nxt;
}e[maxm];
void add_edge(int u,int v,int w){
e[tot].w=w;
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
} bool vis[maxn];
queue<int>que;//队列是点的队列
int d[maxn];
bool spfa(int s){
memset(cnt,,sizeof cnt);
fill(d+,d+n+,inf);
memset(vis,,sizeof vis);
while(!que.empty()) que.pop();
que.push(s);
vis[s]=true;
d[s]=;
while (!que.empty()){
int u=que.front();
que.pop();
vis[u]=false;
for (int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
int w=e[i].w;
if (d[v]>d[u]+w){
d[v]=d[u]+w;
cnt[v]=cnt[u]+;
if(cnt[v]>n) return true;
if (!vis[v]){
vis[v]=true;
que.push(v);
}
}
}
}
return false;
}
int main(){
int a,b,c,T;
scanf("%d",&T);
while(T--){
tot=;
memset(head,-,sizeof head);
memset(vis,,sizeof vis);
memset(d,,sizeof d);
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){//注意为双向边
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
if(c>=)add_edge(b,a,c);
}
if(spfa()) puts("YE5");
else puts("N0");
}
return ;
}

3、bfs_spfa(num>n)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxm=;
const int maxn=;
int head[maxn],tot,n,m,num[maxn],w;
struct edge{
int to;
int w;
int nxt;
}e[maxm];
void add_edge(int u,int v,int w){
e[tot].w=w;
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
} bool vis[maxn];
queue<int>que;//队列是点的队列
int d[maxn];
bool spfa(int s){
memset(num,,sizeof num);
fill(d+,d+n+,inf);
memset(vis,,sizeof vis);
while(!que.empty()) que.pop();
que.push(s);
vis[s]=true;
d[s]=;
while (!que.empty()){
int u=que.front();
que.pop();
vis[u]=false;
for (int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
int w=e[i].w;
if (d[v]>d[u]+w){
d[v]=d[u]+w;
if (!vis[v]){
vis[v]=true;
que.push(v);//hash一下,可判断是否存在负环
num[v]++;
if(num[v]>n) return true;
}
}
}
}
return false;
}
int main(){
int a,b,c,T;
scanf("%d",&T);
while(T--){
tot=;
memset(head,-,sizeof head);
memset(vis,,sizeof vis);
memset(d,,sizeof d);
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){//注意为双向边
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
if(c>=)add_edge(b,a,c);
}
if(spfa()) puts("YE5");
else puts("N0");
}
return ;
}

[luogu3385]dfs_spfa判负环模板的更多相关文章

  1. SPFA判负环模板

    void DFS_SPFA(int u){   if(flag) return; vis[u]=true;   for(int i=head[u];i;i=edges[i].nxt){   if(fl ...

  2. dfs_SPFA 判负环

    感觉有点像tarjan求SCC #include <iostream> #include <cstdio> #include <algorithm> #includ ...

  3. poj3259 Wormholes (判负环)【spfa】(模板)

    <题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...

  4. [模板]SPFA判负环

    目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...

  5. Poj(3259),SPFA,判负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  6. poj3259(spfa判负环)

    题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...

  7. 浅谈SPFA判负环

    目录 SPFA判负环 [前言] [不可代替性] [具体实现] SPFA的过程 判负环 [核心代码] [例题] SPFA判负环 有不足的地方请指出 本蒟蒻一定会修改吼 [前言] 最短路的求法中最广为人知 ...

  8. 【原创】SPFA判负环

    [定义与概念] 给定一张有向图,若其中存在一个环的所有权值之和为负数,这个环称为负环. [算法实现] 当然,负环的求解可以暴搜,但是时间复杂度就难以入眼了,我们回到求解单源最短路径算法上面,看看它们能 ...

  9. SPFA算法的判负环问题(BFS与DFS实现)

    经过笔者的多次实践(失败),在此温馨提示:用SPFA判负环时一定要特别小心! 首先SPFA有BFS和DFS两种实现方式,两者的判负环方式也是不同的.       BFS是用一个num数组,num[x] ...

随机推荐

  1. Python爬虫之利用BeautifulSoup爬取豆瓣小说(一)——设置代理IP

    自己写了一个爬虫爬取豆瓣小说,后来为了应对请求不到数据,增加了请求的头部信息headers,为了应对豆瓣服务器的反爬虫机制:防止请求频率过快而造成“403 forbidden”,乃至封禁本机ip的情况 ...

  2. 2016ACM/ICPC亚洲区沈阳站

    emm,a出3题,补了两题 A,B水题 #include<bits/stdc++.h> #define fi first #define se second #define mp make ...

  3. Sublime Text 3 (含:配置 C# 编译环境)

    Sublime Text 3http://www.sublimetext.com/3http://www.sublimetext.com/3dev 1. 关闭自动更新   菜单:Preferences ...

  4. 51nod 1117 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 1117 聪明的木匠 题目来源: 河北大学算法艺术协会 基准时间限 ...

  5. Android Design TextinputLayout

    使用该布局 需要在build.gradle中的dependencies块中添加两个依赖来向下兼容 dependencies { compile fileTree(dir: 'libs', includ ...

  6. 分布式_理论_01_CAP定理

    一.前言 五.参考资料 1.分布式理论(一) - CAP定理——零壹技术栈 2.分布式理论(一) —— CAP 定理——莫那一鲁道 3.分布式系统理论基础 - CAP 4.分布式系统的CAP理论

  7. jdbc connection为什么放在webINF的lib里面

    jdbc connection为什么放在webINF的lib里面

  8. (转)SPFA算法

    原文地址:http://www.cnblogs.com/scau20110726/archive/2012/11/18/2776124.html 粗略讲讲SPFA算法的原理,SPFA算法是1994年西 ...

  9. Mycat 在vscode中的开发配置

    mycat是国产目前最被追捧的一款分布式数据库集群软件,有一些公司对数据库和应用都有自己的集群方案,但是更多的是一些面对庞大的数据量,而束手无策. 对于这种问题,我想百分之80遇到的是数据库的瓶颈,所 ...

  10. Brackets Sequence(升级版)

    个人心得:又是途径问题,我怕是又炸了.看了题解他的意思就是找出最短的添加顺序的断点,则只要 根据断点添加就好了,注意递归的奥妙之处吧,暂时还真得是拿他没办法. 题目描述: 定义合法的括号序列如下: 1 ...