当前位置:网站首页>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 
边栏推荐
- JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
- Adding database driver to sqoop of cdh6
- Go learning notes - go based interprocess communication
- 线性DP AcWing 895. 最长上升子序列
- LeetCode—剑指 Offer 59 - I、59 - II
- Simple understanding of ThreadLocal
- BOM DOM
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- 区间DP AcWing 282. 石子合并
- 3 A VTT端接 稳压器 NCP51200MNTXG资料
猜你喜欢

BOM DOM

Docker-compose配置Mysql,Redis,MongoDB
![[ybtoj advanced training guidance] cross the river [BFS]](/img/4e/83f14452ea6410768cdd01e725af2e.jpg)
[ybtoj advanced training guidance] cross the river [BFS]

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计

堆 AcWing 838. 堆排序

arcgis js 4. Add pictures to x map
![2.6 using recursion and stack - [tower of Hanoi problem]](/img/fc/45038170dafd104691c93716b103cf.jpg)
2.6 using recursion and stack - [tower of Hanoi problem]

深拷貝 事件總線
随机推荐
spfa AcWing 851. spfa求最短路
Distributed machine learning framework and high-dimensional real-time recommendation system
线性DP AcWing 895. 最长上升子序列
8 examples of using date commands
2.6 using recursion and stack - [tower of Hanoi problem]
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
正确遍历EntryList方法
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
单指令多数据SIMD的SSE/AVX指令集和API
Some sudden program ideas (modular processing)
一些突然迸发出的程序思想(模块化处理)
China traffic sign detection data set
接口测试面试题目,你都会了吗?
async/await 异步函数
C#运算符
ArrayList与LinkedList效率的对比
Anti shake throttle
. Net, C # basic knowledge
[ybtoj advanced training guidance] judgment overflow [error]
