当前位置:网站首页>AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
2022-07-06 18:00:00 【乔大先生】
AcWing 344. 观光之旅
这题是利用floyd的性质求无向图的最小环问题,注意记录路径和求最小中间点的先后顺序和顺序的意义(因为要求的是环,先求环的时候,此时k还未更新成i、j之间最短的点,所以在path[cnt ++ ] = k和下一行的get_min中path[cnt ++ ] = k的k值不同,保证了是一个环)
#include<bits/stdc++.h>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
int n, m;
int d[N][N], w[N][N]; //d储存最短路径,w储存边权值
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); //找到i和中间点k之间经过的点并存入path数组中
path[cnt ++ ] = k;
get_path(k, j); //找到k和j之间经过的点并存入path数组中
}
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 ++ ){
//这里比较疑惑的是为什么先求路径,再找两点之间最短距离
//原因是,此时k还未更新成i、j之间最短的点
//所以在path[cnt ++ ] = k和下一行的get_min中path[cnt ++ ] = k的k值不同,保证了是一个环
//先求路径
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]){
//找到更小环
res = d[i][j] + w[k][i] + w[j][k]; //注意按照环的方向
cnt = 0; //重新记录路径
path[cnt ++ ] = k; //这个环是从k出发
path[cnt ++ ] = i;
get_path(i, j);
path[cnt ++ ] = j;
}
}
}
//求中间点
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;
}
边栏推荐
- 1123. The nearest common ancestor of the deepest leaf node
- Transplant DAC chip mcp4725 to nuc980
- DS-5/RVDS4.0变量初始化错误
- 字节P7专业级讲解:接口测试常用工具及测试方法,福利文
- curl 命令
- 安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
- Taro 小程序开启wxml代码压缩
- Google released a security update to fix 0 days that have been used in chrome
- Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
- go-zero微服务实战系列(九、极致优化秒杀性能)
猜你喜欢
Wood extraction in Halcon
Can the system hibernation file be deleted? How to delete the system hibernation file
Comparison of picture beds of free white whoring
How to manage distributed teams?
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
[case sharing] basic function configuration of network loop detection
如何管理分布式团队?
微信公众号发送模板消息
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
Make Jar, Not War
随机推荐
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
2022 Google CTF segfault Labyrinth WP
Oracle: Practice of CDB restricting PDB resources
uva 1401 dp+Trie
Table table setting fillet
Installation of gazebo & connection with ROS
2022 Google CTF SEGFAULT LABYRINTH wp
数据手册中的词汇
Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
LeetCode:1175. 质数排列
增加 pdf 标题浮窗
Taro applet enables wxml code compression
table表格设置圆角
图片打水印 缩放 和一个输入流的转换
Install Firefox browser on raspberry pie /arm device
THREE.AxesHelper is not a constructor
第三方跳转网站 出现 405 Method Not Allowed
Taro2.* applet configuration sharing wechat circle of friends
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
c语言—数组