当前位置:网站首页>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
边栏推荐
- Docsify deploy IIS
- "As a junior college student, I found out how difficult it is to counter attack after graduation."
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
- Distributed machine learning framework and high-dimensional real-time recommendation system
- Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
- Sse/avx instruction set and API of SIMD
- AI mid stage technology research
猜你喜欢
Find the common ancestor of any two numbers in a binary tree
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
C#修饰符
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
Record the range of data that MySQL update will lock
"As a junior college student, I found out how difficult it is to counter attack after graduation."
MySQL and PostgreSQL methods to grab slow SQL
染色法判定二分图 AcWing 860. 染色法判定二分图
Deep copy event bus
随机推荐
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
Simple use of drools decision table
Enhance network security of kubernetes with cilium
Leetcode - Sword finger offer 51 Reverse pairs in an array
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
Go learning notes - go based interprocess communication
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
绕过ObRegisterCallbacks需要驱动签名方法
染色法判定二分图 AcWing 860. 染色法判定二分图
ASP. Net MVC default configuration, if any, jumps to the corresponding program in the specified area
Drools executes the specified rule
C#修饰符
基于STM32的OLED 屏幕驱动
[ybtoj advanced training guide] similar string [string] [simulation]
计数类DP AcWing 900. 整数划分
Simple understanding of ThreadLocal
H5 to app
通过反射执行任意类的任意方法
AI中台技术调研