负环--spfa
负环?是有负权边的环还是一个边权之和为负的环?
还没有准确的定义(那就先忽略吧qwq
判断负环的方法:
暴力枚举/spfa/mellman—ford/奇怪的贪心/超神的搜索
可惜我只会spfa
spfa:垂死病中惊坐起
有两种spfa可以用来求负环:dfs和bfs
在求负环上bfs要更好一些,dfs稍逊色一些
(注意:要memset,变量类型定义的时候要细致一些)
//bfs版spfa
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define ll long long
using namespace std; inline ll read()
{
ll sum = , p = ;
char ch = getchar();
while(ch < '' || ch > '')
{
if(ch == '-')
p = -;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
(sum *= ) += ch - '';
ch = getchar();
}
return sum * p;
} const int maxn = ,maxm = ;
struct edge
{
int nxt,to;
ll wei;
} e[maxm * ];
int t,n,m,tot,cnt[maxn],head[maxn],dis[maxn];
bool vis[maxn]; void add(int a,int b,ll c)
{
e[++tot].nxt = head[a];
e[tot].to = b;
e[tot].wei = c;
head[a] = tot;
} bool spfa(int x)
{
queue<int> q;
for(int i = ; i <= n; i++)
dis[i] = 1e9;
vis[x] = true;
q.push(x);
cnt[x]++;
dis[x] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
if(cnt[u] >= n)
return true;
for(int i = head[u]; i; i = e[i].nxt)
{ int v = e[i].to;
if(dis[v] > dis[u] + e[i].wei)
{
dis[v] = dis[u] + e[i].wei;
if(!vis[v])
{
vis[v] = true;
q.push(v);
cnt[v] ++;
if(cnt[v] >= n)
return true;
}
}
}
}
return false;
} int main()
{
t = read();
while(t--)
{
n = read(),m = read();
tot = ;
memset(e,,sizeof(e));
memset(dis,,sizeof(dis));
memset(head,,sizeof(head));
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt));
int a,b,c;
for(int i = ; i <= m; i++)
{
a = read(),b = read(),c = read();
add(a,b,c);
if(c >= )
add(b,a,c);
}
if(spfa())
printf("YE5\n");
else
printf("N0\n");
}
return ;
}
//dfs版spfa
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; inline int read()
{
int sum = , p = ;
char ch = getchar();
while(ch < '' || ch > '')
{
if(ch == '-')
p = -;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
(sum *= ) += ch - '';
ch = getchar();
}
return sum * p;
} const int maxn = ,maxm = ;
bool flag,vis[maxn];
int n,m,t;
int head[maxn],dis[maxn],cnt;
struct edge
{
int nxt,to,wei;
}e[maxm * ]; void add(int a,int b,int c)
{
e[++cnt].nxt = head[a];
e[cnt].to = b;
e[cnt].wei = c;
head[a] = cnt;
} void spfa(int x)
{
vis[x] = true;
for(int i = head[x];i;i = e[i].nxt)
{
int v = e[i].to;
if(dis[v] > dis[x] +e[i].wei)
{
if(vis[v] || flag)
{
flag = true;
break;
}
dis[v] = dis[x] +e[i].wei;
spfa(v);
}
}
vis[x] = false;
} int main()
{
t = read();
while(t--)
{
cnt = ;
flag = ;
memset(head,,sizeof(head));
memset(dis,,sizeof(dis));
memset(e,,sizeof(e));
memset(vis,false,sizeof(vis));
n = read(),m = read();
int a,b,c;
for(int i = ;i <= m;i++)
{
a = read(),b = read(),c = read();
add(a,b,c);
if(c >= )
add(b,a,c);
}
for(int i = ;i <= n;i++)
{
spfa(i);
if(flag)
break;
}
if(flag)
printf("YE5\n");
else
printf("N0\n");
}
return ;
}
负环--spfa的更多相关文章
- [P3385]【模板】负环 (spfa / bellman-ford)
终于开始认真对待图论了 因为听说一直是提高组的,动得很少,直到现在机房打提高的氛围下,开始学一些皮毛的东西 模板题目链接 这是一道求负环的题目,照理来说大家都是用spfa来判断负环的 但是我觉得bel ...
- luogu3385 负环 (spfa)
我在做spfa的时候,如果有一个点被更新了超过N次,证明这个图里是有负环的. (神TM输出YE5和N0) #include<bits/stdc++.h> #define pa pair&l ...
- 【BZOJ4773】负环 [SPFA][二分]
负环 Time Limit: 100 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 在忘记考虑负环之后,黎瑟的算法又出错 ...
- 洛谷P3385 [模板]负环 [SPFA]
题目传送门 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个 ...
- Spfa【p3385】【模板】负环(spfa)
顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述 毒瘤数据要求判负环 分析: 还是融合了不少题解的思想的. 负环定义: 权值和为负的环 //在网络上并没有找到一个官方定义,暂且这么理解. ...
- 洛谷P3385判负环——spfa
题目:https://www.luogu.org/problemnew/show/P3385 两种方法,dfs和bfs: 一开始写的dfs,要把dis数组初值赋成0,这样从一个连着负边的点开始搜: 在 ...
- LG P2285 [模板]负环(spfa判负环)
题目描述 寻找一个从顶点1所能到达的负环,负环定义为:一个边权之和为负的环. 输入格式 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 接下来M行,每 ...
- 洛谷 P3385 【模板】负环 (SPFA)
题意:有一个\(n\)个点的有向图,从\(1\)出发,问是否有负环. 题解:我们可以用SPFA来进行判断,在更新边的时候,同时更新路径的边数,因为假如有负环的话,SPFA这个过程一定会无限重复的遍历这 ...
- UVA 558 Wormholes 【SPFA 判负环】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
随机推荐
- jenkins pipline 如何禁止任务并行
背景: 我测试的一个项目CI包括好几个步骤,但是有的步骤是不能并行的,否则会互相影响 处理过程: [方案一]:不推荐此方案 在每个步骤里面的shell脚本中加进程判断 示例:比如本任务有4个步骤,第2 ...
- C++-POJ2975-Nim
题目把Nim游戏为什么可以取异或和讲解得十分清楚,建议多读几次,理解一下 再一个,可以把每次异或视为一次取数,因此(k[i]^sg)<k[i]即为一种可行操作 /* Nim is a 2-pla ...
- No module named ‘sklearn.model_selection解决办法
在python中运行导入以下模块 from sklearn.model_selection import train_test_split 出现错误: No module named ‘sklear ...
- 自己动手系列----使用数组实现一个简单的Set
Set:注重独一无二的性质,该体系集合可以知道某物是否已近存在于集合中,不会存储重复的元素用于存储无序(存入和取出的顺序不一定相同)元素,值不能重复.主要有HashSet和TreeSet两大实现类. ...
- echart--自己写的例子
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 分析https网页加载http资源导致的页面报错原因及其解决方案
https网页加载http资源导致的页面报错及解决方案 https是当下的网站的主流趋势,甚至像苹果这样的大公司,则完全要求用户必须使用https地址. 然而对于以前http链接来说,我们往往就存在一 ...
- codeforces div2_603 F. Economic Difficulties(树dfs预处理+dp)
题目连接:http://codeforces.com/contest/1263/problem/F 题意:有n个设备,上和下分别连接着一颗树,上下两棵树每棵树的叶子节点连接一个设备,两棵树的根节点都是 ...
- .Net Core初体验
对于C#语言支持(由C#1.0-C#7.1): 编码可以使用跨平台的IDE选择,就如同VS+Resharper一样方便: 运行效果:
- 计数器IP核
Quartus II提供的LPM_couter IP核的使用 FPGA设计方式: 原理图,Verilog HDL设计方式,IP核输入方式 创建IP核 点击TOOLS—IP catalog-libray ...
- 图解SOAPUI解析WSDL文件
本文链接:https://blog.csdn.net/qq_16234613/article/details/53143279 新建项目 添加WSDL文件 查看方法 查看XML格式 运行测试