当前位置:网站首页>AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
2022-07-07 01:36:00 【Mr. Qiao Da】
AcWing 1148. Secret milk transport
Their thinking : First find the minimum spanning tree , Calculate the sum of the weights and mark all the edges in the minimum spanning tree . Then traverse all edges , Find the largest edge and the second largest edge on the path containing two points and record ,( When two points find a new path to connect , Only which largest side is disconnected (t = sum - dist1[a][b] + w,dist1 Is the largest edge , It is concluded that the t Will be a smaller value , Then we can get the second smallest spanning tree ), To ensure that the next small spanning tree is found , So record the largest edge , The second largest edge is recorded to prevent the largest edge from being used ), Then traverse all the edges in the non minimum spanning tree , Replace the edges in the tree in turn , Finally, we find the second smallest spanning tree 
Big guy problem solving original address
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 510, M = 2e4 + 10;
int n, m;
int p[N]; // Union checking set
int dist1[N][N], dist2[N][N]; // Record the maximum value and the second maximum value on the corresponding edge path respectively
ll res = 1e18; // The final answer
ll sum; // Record the weight of the minimum spanning tree
int h[N], e[M], ne[M], w[M], idx; // Record tree
struct Edge{
int a, b, w;
bool f; // Mark whether this edge is in the minimum spanning tree
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[]){
// Find the maximum value and the second largest value on the path corresponding to each point
d1[u] = mad1, d2[u] = mad2; //d1、d2 Array has no assignment before
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};
}
// Find the smallest spanning tree
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; // Here we need to use points to build maps , therefore a、b Are used to express themselves
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;
}
}
// Find the maximum value and the second largest value of the path corresponding to each point
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;
}
边栏推荐
- HMM notes
- AI 从代码中自动生成注释文档
- Send template message via wechat official account
- What does security capability mean? What are the protection capabilities of different levels of ISO?
- LeetCode. 剑指offer 62. 圆圈中最后剩下的数
- 1123. 最深叶节点的最近公共祖先
- Neon Optimization: performance optimization FAQ QA
- 长按按钮执行函数
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- Let's see through the network i/o model from beginning to end
猜你喜欢

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

Transplant DAC chip mcp4725 to nuc980

鼠标右键 自定义

Appium automation test foundation uiautomatorviewer positioning tool
![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]

Gazebo的安装&与ROS的连接

1123. The nearest common ancestor of the deepest leaf node

Gin 入门实战

第三方跳转网站 出现 405 Method Not Allowed

MySQL script batch queries all tables containing specified field types in the database
随机推荐
Let's see through the network i/o model from beginning to end
454-百度面经1
Spark TPCDS Data Gen
Yunna - work order management system and process, work order management specification
Let's see how to realize BP neural network in Matlab toolbox
Neon Optimization: an optimization case of log10 function
C语言实例_4
Gazebo的安装&与ROS的连接
糊涂工具类(hutool)post请求设置body参数为json数据
搭建【Redis in CentOS7.x】
405 method not allowed appears when the third party jumps to the website
shell脚本快速统计项目代码行数
mysqlbackup 还原特定的表
THREE. AxesHelper is not a constructor
使用nodejs完成判断哪些项目打包+发版
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
Yunna | work order management measures, how to carry out work order management
Appium foundation - appium inspector positioning tool (I)
7.6模拟赛总结
AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)