当前位置:网站首页>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 
边栏推荐
- Dijkstra AcWing 850. Dijkstra求最短路 II
- Docsify deploy IIS
- 线性DP AcWing 898. 数字三角形
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- Redis bloom filter
- Bom Dom
- Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
- [ybtoj advanced training guidance] judgment overflow [error]
- bellman-ford AcWing 853. 有边数限制的最短路
- 线性DP AcWing 897. 最长公共子序列
猜你喜欢

Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)

NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF

async/await 异步函数

JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)

spfa AcWing 852. spfa判断负环

js1day(输入输出语法,数据类型,数据类型转换,var和let区别)

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution

Simple use of drools decision table

Use sqoop to export ads layer data to MySQL

应用LNK306GN-TL 转换器、非隔离电源
随机推荐
Performance tuning project case
Efficiency comparison between ArrayList and LinkedList
Redis sentinel mechanism and configuration
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
绕过ObRegisterCallbacks需要驱动签名方法
中国交通标志检测数据集
Anti shake throttle
Mongodb redis differences
LeetCode—<动态规划专项>剑指 Offer 19、49、60
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
深拷贝 事件总线
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
堆 AcWing 839. 模拟堆
Adding database driver to sqoop of cdh6
基于STM32的OLED 屏幕驱动
Sweetheart leader: Wang Xinling
FBX import under ue4/ue5 runtime
Some sudden program ideas (modular processing)
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
Simple understanding of ThreadLocal
