当前位置:网站首页>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;
}
边栏推荐
- 修改px4飞控的系统时间
- JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
- Neon Optimization: summary of performance optimization experience
- C# 计算农历日期方法 2022
- Using the entry level of DVA in taro3.*
- 自旋与sleep的区别
- 负载均衡性能参数如何测评?
- 2022 Google CTF segfault Labyrinth WP
- 移植DAC芯片MCP4725驱动到NUC980
- Yunna - work order management system and process, work order management specification
猜你喜欢

从底层结构开始学习FPGA----FIFO IP的定制与测试

Installation of gazebo & connection with ROS
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

AI 从代码中自动生成注释文档

今日问题-2022/7/4 lambda体中修改String引用类型变量

c语言—数组
![[signal and system]](/img/aa/a65d6da1d1d9410254ca7b775e24a6.png)
[signal and system]

golang中的Mutex原理解析

Yunna | work order management measures, how to carry out work order management

shell脚本快速统计项目代码行数
随机推荐
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
Neon Optimization: an optimization case of log10 function
力扣1037. 有效的回旋镖
让我们,从头到尾,通透网络I/O模型
Send template message via wechat official account
AI automatically generates annotation documents from code
C language instance_ three
LeetCode:1175. Prime permutation
HMM notes
go-zero微服务实战系列(九、极致优化秒杀性能)
自旋与sleep的区别
Taro 小程序开启wxml代码压缩
POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
Wood extraction in Halcon
THREE. AxesHelper is not a constructor
Using the entry level of DVA in taro3.*
json学习初体验–第三者jar包实现bean、List、map创json格式
Table table setting fillet
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
NEON优化:矩阵转置的指令优化案例