当前位置:网站首页>spfa AcWing 852. SPFA judgement negative ring
spfa AcWing 852. SPFA judgement negative ring
2022-07-02 12:42:00 【T_ Y_ F666】
spfa AcWing 852. spfa Judge negative loop
Original link
AcWing 852. spfa Judge negative loop
Algorithm tags
Negative ring determination spfa
Ideas
Code
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 100005;
// cnt[i] Store the i Nodes need to pass through the number of edges
int h[N], e[N], ne[N], w[N], idx, cnt[N];
int dis[N];
bool st[N];
int n,m;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
void add(int a, int b, int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
bool sp(){
queue<int> q;
// The problem is to judge whether there is a negative ring
// So you need to queue all the points , Update the surrounding points
rep(i, 1, n+1){
st[i]=true;
q.push(i);
}
while(q.size()){
int t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(dis[j]>dis[t]+w[i]){
dis[j]=dis[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=n){
return true;
}
if(!st[j]){
q.push(j);
st[j]=true;
}
}
}
}
return false;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(h, -1, sizeof h);
n=read(), m=read();
while(m--){
int x=read(), y=read(), z=read();
add(x, y, z);
}
if(sp()){
puts("Yes");
}else{
puts("No");
}
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- Input box assembly of the shutter package
- Is the neural network (pinn) with embedded physical knowledge a pit?
- Redis sentinel mechanism and configuration
- Floyd AcWing 854. Floyd求最短路
- 模块化 CommonJS ES Module
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
- Simple understanding of ThreadLocal
- Mui WebView down refresh pull-up load implementation
- Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
- Performance tuning project case
猜你喜欢
![[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol](/img/13/9002244555ebe8a61660c2506993fa.png)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol

js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)

There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes

LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

C#运算符

BOM DOM

"As a junior college student, I found out how difficult it is to counter attack after graduation."

AI mid stage technology research
随机推荐
正确遍历EntryList方法
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
BOM DOM
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
H5 to app
Use sqoop to export ads layer data to MySQL
About the loading of layer web spring layer components, the position of the layer is centered
深拷貝 事件總線
2.7 binary tree, post order traversal - [FBI tree]
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
Writing method of then part in drools
高性能纠删码编码
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
BOM DOM
Mongodb redis differences
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
CPU指令集介绍
