当前位置:网站首页>AcWing 904. Wormhole solution (SPFA for negative rings)
AcWing 904. Wormhole solution (SPFA for negative rings)
2022-07-07 01:36:00 【Mr. Qiao Da】
AcWing 904. Wormhole
A negative ring is a ring in which the sum of the weights in the graph is negative , If there is a negative ring in the graph , Then the farmer must be able to return to the starting point , On the contrary, it must not , So this is a naked problem of finding negative rings . use spfa Record the number of edges on the shortest path , If the number of edges on a path is greater than or equal to n, Then it can be determined that there is a negative ring on this road , return true that will do , Instead, return to 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]; // Shortest distance record array
int cnt[N]; // Record the number of sides of the shortest distance
void add(int a, int b, int c){
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx ++ ;
}
bool spfa(){
// Judge negative loop
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; // from t To j One more side w[t][j]
if(cnt[j] >= n) return true; // If the number of edges on this path is greater than n, That means there is a negative ring , The farmer must be able to return to the starting point
if(!st[j]){
q.push(j);
st[j] = true;
}
}
}
}
return false; // If you haven't found a negative ring , It means that the farmer cannot return to the starting point , return false
}
int main()
{
cin>>T;
while(T -- ){
cin>>n>>m1>>m2;
memset(h, -1, sizeof h);
idx = 0; // Remember to initialize
for(int i = 0; i < m1; i ++ ){
int a, b, c;
cin>>a>>b>>c;
// Normal path , Equivalent to Bidirectional Transformation
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); // Wormhole is equivalent to a negative weight edge
}
if(spfa()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
边栏推荐
- AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
- Gin introduction practice
- Neon Optimization: an optimization case of log10 function
- Can the system hibernation file be deleted? How to delete the system hibernation file
- 设置Wordpress伪静态连接(无宝塔)
- 域分析工具BloodHound的使用说明
- 使用nodejs完成判断哪些项目打包+发版
- C语言实例_3
- HMM notes
- DS-5/RVDS4.0变量初始化错误
猜你喜欢

AcWing 361. 观光奶牛 题解(spfa求正环)

云呐|工单管理软件,工单管理软件APP

Let's see through the network i/o model from beginning to end

Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period

2022 Google CTF SEGFAULT LABYRINTH wp

Body mass index program, entry to write dead applet project

修改px4飞控的系统时间

Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman

Go zero micro service practical series (IX. ultimate optimization of seckill performance)

How to manage distributed teams?
随机推荐
Taro2.* 小程序配置分享微信朋友圈
Spark TPCDS Data Gen
Table table setting fillet
数据手册中的词汇
剑指 Offer II 035. 最小时间差-快速排序加数据转换
dvajs的基础介绍及使用
云呐-工单管理制度及流程,工单管理规范
AcWing 1142. 繁忙的都市 题解(最小生成树)
curl 命令
THREE. AxesHelper is not a constructor
一起看看matlab工具箱内部是如何实现BP神经网络的
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
Using the entry level of DVA in taro3.*
Transplant DAC chip mcp4725 to nuc980
405 method not allowed appears when the third party jumps to the website
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Neon Optimization: performance optimization FAQ QA
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
shell脚本快速统计项目代码行数