当前位置:网站首页>AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
2022-07-07 01:36:00 【Mr. Qiao Da】
AcWing 344. Sightseeing tour
Use this question floyd To find the minimum ring problem of undirected graphs , Pay attention to the sequence and significance of recording the path and finding the minimum intermediate point ( Because what is required is a ring , When you first find the ring , here k Not updated to i、j The shortest point between , So in path[cnt ++ ] = k And the next line get_min in path[cnt ++ ] = k Of k Values are different , It is guaranteed to be a ring )
#include<bits/stdc++.h>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
int n, m;
int d[N][N], w[N][N]; //d Store the shortest path ,w Store edge weights
int path[N];
int pos[N][N];
int cnt;
void get_path(int i, int j){
if(pos[i][j] == 0) return ;
int k = pos[i][j];
get_path(i, k); // find i And the middle point k And save path Array
path[cnt ++ ] = k;
get_path(k, j); // find k and j And save path Array
}
signed main()
{
cin>>n>>m;
memset(w, 0x3f, sizeof w);
for(int i = 1; i <= n; i ++ ) w[i][i] = 0;
while(m -- ){
int a, b, c;
cin>>a>>b>>c;
w[a][b] = w[b][a] = min(w[a][b], c);
}
int res = INF;
memcpy(d, w, sizeof d);
for(int k = 1; k <= n; k ++ ){
// What is more puzzling here is why to seek the path first , Find the shortest distance between two points
// as a result of , here k Not updated to i、j The shortest point between
// So in path[cnt ++ ] = k And the next line get_min in path[cnt ++ ] = k Of k Values are different , It is guaranteed to be a ring
// Find the path first
for(int i = 1; i < k; i ++ ){
for(int j = i + 1; j < k; j ++ ){
if(res > (long long)d[i][j] + w[k][i] + w[j][k]){
// Find a smaller ring
res = d[i][j] + w[k][i] + w[j][k]; // Pay attention to the direction of the ring
cnt = 0; // Re record the path
path[cnt ++ ] = k; // This ring is from k set out
path[cnt ++ ] = i;
get_path(i, j);
path[cnt ++ ] = j;
}
}
}
// Find the middle point
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
if(d[i][j] > d[i][k] + d[k][j]){
d[i][j] = d[i][k] + d[k][j];
pos[i][j] = k;
}
}
}
}
if(res == INF) cout<<"No solution."<<endl;
else{
for(int i = 0; i < cnt; i ++ ) cout<<path[i]<<' ';
cout<<endl;
}
return 0;
}
边栏推荐
- [JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
- How to prevent overfitting in cross validation
- C语言实例_3
- 云呐-工单管理制度及流程,工单管理规范
- ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
- tansig和logsig的差异,为什么BP喜欢用tansig
- Neon Optimization: an instruction optimization case of matrix transpose
- 使用nodejs完成判断哪些项目打包+发版
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- Neon Optimization: an optimization case of log10 function
猜你喜欢

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which

shell脚本快速统计项目代码行数

黑马笔记---异常处理

js如何快速创建一个长度为 n 的数组

盒子拉伸拉扯(左右模式)

Typical problems of subnet division and super network construction

dvajs的基础介绍及使用

According to the analysis of the Internet industry in 2022, how to choose a suitable position?

mongodb查看表是否导入成功

MySQL script batch queries all tables containing specified field types in the database
随机推荐
Installation of gazebo & connection with ROS
7.6 simulation summary
go-zero微服务实战系列(九、极致优化秒杀性能)
Neon Optimization: summary of performance optimization experience
1123. 最深叶节点的最近公共祖先
uva 1401 dp+Trie
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
C language - array
Receive user input, height BMI, BMI detection small business entry case
Spark TPCDS Data Gen
Today's question -2022/7/4 modify string reference type variables in lambda body
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
Gnet: notes on the use of a lightweight and high-performance go network framework
Google发布安全更新,修复Chrome中已被利用的0 day
Taro2.* 小程序配置分享微信朋友圈
使用nodejs完成判断哪些项目打包+发版
table表格设置圆角
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
数据手册中的词汇