当前位置:网站首页>AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
2022-07-06 18:00:00 【乔大先生】
AcWing 1148. 秘密的牛奶运输
解题思路:先找到最小生成树,计算出权值之和并标记所有最小生成树内的边。之后遍历所有边,找到包含两个点的路径的上的最大边和次大边并记录,(当两个点找到一条新的路径相连时,只有断开哪个最大边(t = sum - dist1[a][b] + w,dist1是最大边,得出的t才会是更小值,才能得出次小生成树),才能保证找到的是次小生成树,所以要记录最大边,记录次大边是为了防止最大边已经被用过),之后遍历所有非最小生成树内的边,依次替换树内的边,最终找到次小生成树
大佬题解原地址
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 510, M = 2e4 + 10;
int n, m;
int p[N]; //并查集
int dist1[N][N], dist2[N][N]; //分别记录对应边路径上的最大值和次大值
ll res = 1e18; //最终答案
ll sum; // 记录最小生成树的权值
int h[N], e[M], ne[M], w[M], idx; //记录树
struct Edge{
int a, b, w;
bool f; //标记这个边是否在最小生成树内
bool operator< (const Edge &t) const {
return w < t.w;
}
}edg[M];
void add(int a, int b, int c){
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx ++ ;
}
int find(int x){
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
void dfs(int u, int fa, int mad1, int mad2, int d1[], int d2[]){
//找到每个点对应路径上个最大值和次大值
d1[u] = mad1, d2[u] = mad2; //d1、d2数组在此之前没有赋值
for(int i = h[u]; ~i; i = ne[i]){
int j = e[i];
if(j != fa){
int td1 = mad1, td2 = mad2;
if(w[i] > td1){
td2 = td1;
td1 = w[i];
}
else if(w[i] < td1 && w[i] > td2){
td2 = w[i];
}
dfs(j, u, td1, td2, d1, d2);
}
}
}
signed main()
{
cin>>n>>m;
memset(h, -1, sizeof h);
for(int i = 0; i < m; i ++ ){
int a, b, c;
cin>>a>>b>>c;
edg[i] = {
a, b, c};
}
//找到最小生成树
sort(edg, edg + m);
for(int i = 1; i <= n; i ++ ) p[i] = i;
for(int i = 0; i < m; i ++ ){
int a = edg[i].a, b = edg[i].b, w = edg[i].w; //这里要利用点去建图,所以a、b均用于表示自己
int pa = find(a), pb = find(b);
if(pa != pb){
sum += w;
p[pa] = pb;
add(a, b, w);
add(b, a, w);
edg[i].f = true;
}
}
//找到每个点对应路径的最大值和次大值
for(int i = 1; i <= n; i ++ ) dfs(i, -1, -1e9, -1e9, dist1[i], dist2[i]);
for(int i = 0; i < m; i ++ ){
if(!edg[i].f){
int a = edg[i].a, b = edg[i].b, w = edg[i].w;
ll t;
if(w > dist1[a][b]){
t = sum - dist1[a][b] + w;
}
else if(w > dist2[a][b]){
t = sum - dist2[a][b] + w;
}
res = min(res, t);
}
}
cout<<res<<endl;
return 0;
}
边栏推荐
- MySQL中回表的代价
- Atomic in golang, and cas Operations
- 一起看看matlab工具箱内部是如何实现BP神经网络的
- Instructions for using the domain analysis tool bloodhound
- Installation and testing of pyflink
- Let's see through the network i/o model from beginning to end
- 2022 Google CTF SEGFAULT LABYRINTH wp
- 剑指 Offer II 035. 最小时间差-快速排序加数据转换
- 编译命令行终端 swift
- [advanced C language] 8 written questions of pointer
猜你喜欢

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

Gazebo的安装&与ROS的连接

力扣1037. 有效的回旋镖
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

微信公众号发送模板消息

一起看看matlab工具箱内部是如何实现BP神经网络的

身体质量指数程序,入门写死的小程序项目

系统休眠文件可以删除吗 系统休眠文件怎么删除

Installation of gazebo & connection with ROS

Analysis of mutex principle in golang
随机推荐
tansig和logsig的差异,为什么BP喜欢用tansig
Gazebo的安装&与ROS的连接
Metauniverse urban legend 02: metaphor of the number one player
【C语言进阶篇】指针的8道笔试题
2022 Google CTF SEGFAULT LABYRINTH wp
Oracle:CDB限制PDB资源实战
Add the applet "lazycodeloading": "requiredcomponents" in taro,
Yunna - work order management system and process, work order management specification
第三方跳转网站 出现 405 Method Not Allowed
1123. The nearest common ancestor of the deepest leaf node
MySQL script batch queries all tables containing specified field types in the database
Using the entry level of DVA in taro3.*
Taro 小程序开启wxml代码压缩
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
Boot - Prometheus push gateway use
Transplant DAC chip mcp4725 to nuc980
MySQL中回表的代价
接收用户输入,身高BMI体重指数检测小业务入门案例
golang 基础 —— 数据类型
swiper组件中使用video导致全屏错位