当前位置:网站首页>AcWing 904. 虫洞 题解(spfa求负环)
AcWing 904. 虫洞 题解(spfa求负环)
2022-07-06 18:00:00 【乔大先生】
AcWing 904. 虫洞
负环就是图中存在的权值之和为负数的环,如果图中存在负环,则农夫一定能返回起点,反之一定不能,所以这是一个裸的求负环的题目。用spfa记录最短路上的边的数量,如果某条路径上边的数量大于等于n,则可以认定这条路上存在负环,返回true即可,反之返回false
#include<bits/stdc++.h>
using namespace std;
const int N = 520, M = 5210;
int T, n, m1, m2;
int h[N], ne[M], e[M], w[M], idx;
int st[N];
int dist[N]; //最短距离记录数组
int cnt[N]; //记录最短距离的边数
void add(int a, int b, int c){
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx ++ ;
}
bool spfa(){
//判断负环
memset(dist, 0, sizeof dist);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
queue<int>q;
for(int i = 1; i <= n; i ++ ){
q.push(i);
st[i] = true;
}
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1; //从t到j多了一条边w[t][j]
if(cnt[j] >= n) return true; //如果这个路径上的边的数量大于n,那说明存在负环,农夫一定可以回到起点
if(!st[j]){
q.push(j);
st[j] = true;
}
}
}
}
return false; //如果一直没有找到负环,说明农夫回不到起点,返回false
}
int main()
{
cin>>T;
while(T -- ){
cin>>n>>m1>>m2;
memset(h, -1, sizeof h);
idx = 0; //记住初始化
for(int i = 0; i < m1; i ++ ){
int a, b, c;
cin>>a>>b>>c;
//正常路径,相当于双向变
add(a, b, c);
add(b, a, c);
}
for(int i = 0; i < m2; i ++ ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, -c); //虫洞相当于一条负权边
}
if(spfa()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
边栏推荐
- Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
- Dark horse notes - create immutable sets and streams
- Install Firefox browser on raspberry pie /arm device
- 2022 Google CTF SEGFAULT LABYRINTH wp
- LeetCode:1175. Prime permutation
- C language - array
- DS-5/RVDS4.0变量初始化错误
- 736. Lisp 语法解析 : DFS 模拟题
- AI 从代码中自动生成注释文档
- How to prevent overfitting in cross validation
猜你喜欢

JTAG principle of arm bare board debugging
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

Dark horse notes - create immutable sets and streams

C language - array

云呐|工单管理办法,如何开展工单管理

1123. The nearest common ancestor of the deepest leaf node

2022 Google CTF SEGFAULT LABYRINTH wp

Make Jar, Not War
![[signal and system]](/img/aa/a65d6da1d1d9410254ca7b775e24a6.png)
[signal and system]

子网划分、构造超网 典型题
随机推荐
黑马笔记---创建不可变集合与Stream流
域分析工具BloodHound的使用说明
DS-5/RVDS4.0变量初始化错误
C language instance_ two
从零开始匹配vim(0)——vimscript 简介
子网划分、构造超网 典型题
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
swiper组件中使用video导致全屏错位
Analysis of mutex principle in golang
云呐|工单管理软件,工单管理软件APP
THREE. AxesHelper is not a constructor
公钥\私人 ssh避password登陆
Neon Optimization: summary of performance optimization experience
C语言实例_4
Yunna | work order management software, work order management software app
Neon Optimization: About Cross access and reverse cross access
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
负载均衡性能参数如何测评?
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Js逆向——捅了【马蜂窝】的ob混淆与加速乐