当前位置:网站首页>Minor spanning tree
Minor spanning tree
2022-07-05 04:37:00 【Bright autumn leaves】
. Let's find the minimum spanning tree first , Then enumerate the non tree edges in turn , Then add the edge to the tree , At the same time, remove an edge from the tree , So that the final picture is still a tree , Statistical minimum .
This method must be able to find non strict . Prove the following
Definition 1: set up T Figure G A spanning tree of , For non tree edges a And by the tree b, Insert edge a, And delete the edge b The operation of is recorded as (+a, -b). If T+a-b after , It's still a spanning tree , call (+a,-b) yes T A viable exchange for .
Definition 2: Called by T The new set of spanning trees obtained by a feasible transformation is called T Adjacent sets of .
Enumerating external edges in practice , Deleting an edge in a tree is enumeration MGT Adjacent sets of .
Theorem : The next smallest spanning tree must be in the neighborhood of the smallest spanning tree
Theorem proving :
Counter evidence , If there is a small spanning tree that is different from the minimum spanning tree k side , Sort do kruskal, Find the first side different from the second side , On the edge of the tree t Connect ab.
We will connect secondary schools ab One of the sides p Replace with t, Then small will become smaller ( Because of the order kruskal) For such a feasible exchange, we let the number of sides of the sub small spanning tree and the minimum spanning tree be different k-1. This operation can be continued until only 1 side . So the next smallest spanning tree must be in the adjacent set of the smallest spanning tree .
// New spanning tree newsum = sum - w( Inside the tree 2 spot The edge between ) + w( Outside the tree 2 The edge between points )
//(1) if newsum < sum ( be sum It must not be the minimum spanning tree edge weight sum contradiction )
// therefore sum > newsum ( Strictly sub small spanning trees )
//(2) if w( Outside the tree 2 The edge between points ) (a - b Two swords ) Not the biggest that Minimum spanning tree You can definitely get to this side
// contradiction
// therefore Be sure to get The largest side between two points Come on Replace new edge
// If new edge == The largest side between two points be Take the second largest
```
#include <bits/stdc++.h>
using namespace std;
#define io std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define x first
#define y second
#define fr front
#define db double
//int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
//int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
//int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
//int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
//int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
typedef pair<int, int> PII;
typedef long long LL ;
const int N = 10010, M = 510;
struct edge
{
int a, b, c, f;
bool operator< (edge x)
{
return c < x.c;
}
}g[N];
int n, m;
int h[M], e[N], w[N], ne[N], idx;// Store the minimum spanning tree
int dist1[M][M], dist2[M][M];// The farthest distance between two points in the tree And Second distance
int p[M];
void add(int a, int b, int c) // Add an edge a->b, The boundary right is c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
int find(int x) // Union checking set
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
void dfs(int u, int f, int m1, int m2, int ds1[], int ds2[])
{
ds1[u] = m1, ds2[u] = m2;
for (int i = h[u]; ~i ;i = ne[i])
{
int t = e[i];
if (t != f)
{
int td1 = m1, td2 = m2;
if (w[i] > td1) td2 = td1, td1 = w[i];
else if (w[i] < td1 && w[i] > td2) td2 = w[i];
dfs(t, u, td1, td2, ds1, ds2);
}
}
}
int main()
{
io;
memset(h, -1, sizeof h);
cin >> n >> m;
for (int i = 0;i < m;i ++ )
{
int a, b, c, f = 0;
cin >> a >> b >> c;
g[i] = {a, b, c, f};
}
for (int i = 1;i <= n;i ++ ) p[i] = i;
// Find the minimum spanning tree
sort(g, g + m);
LL sum = 0;
for (int i = 0;i < m;i ++ )
{
int a = g[i].a, b = g[i].b, c = g[i].c;
int x = find(a), y = find(b);
if (x != y)
{
add(a, b, c), add(b, a, c); // Store the second smallest spanning tree
g[i].f = 1; // Mark in the tree
p[x] = y;
sum += c;
}
}
// New spanning tree newsum = sum - w( Inside the tree 2 spot The edge between ) + w( Outside the tree 2 The edge between points )
//(1) if newsum < sum ( be sum It must not be the minimum spanning tree edge weight sum contradiction )
// therefore sum > newsum ( Strictly sub small spanning trees )
//(2) if w( Outside the tree 2 The edge between points ) (a - b Two swords ) Not the biggest that Minimum spanning tree You can definitely get to this side
// contradiction
// therefore Be sure to get The largest side between two points Come on Replace new edge
// If new edge == The largest side between two points be Take the second largest
// Find in the spanning tree The farthest distance between two points And Second distance
for (int i = 1;i <= n;i ++ ) dfs(i, -1, -1e9, -1e9, dist1[i], dist2[i]);
LL ans = 1e18;
for (int i = 0;i < m;i ++ )
if (!g[i].f) // Not only the edges in the minimum spanning tree
{
int a = g[i].a, b = g[i].b, c = g[i].c;
LL t;
if (c > dist1[a][b])
t = sum + c - dist1[a][b];
else if (c > dist2[a][b])
t = sum + c - dist2[a][b];
ans = min(ans, t);
}
cout << ans << endl;
return 0;
}
边栏推荐
- Function (basic: parameter, return value)
- 取余操作是一个哈希函数
- mxnet导入报各种libcudart*.so、 libcuda*.so找不到
- [crampon game] MC tutorial - first day of survival
- 2022-2028 global and Chinese virtual data storage Market Research Report
- [uniapp] system hot update implementation ideas
- 【虚幻引擎UE】实现测绘三脚架展开动画制作
- Official announcement! The third cloud native programming challenge is officially launched!
- Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
- [finebi] the process of making custom maps using finebi
猜你喜欢
Fonction (sujette aux erreurs)
Live broadcast preview | container service ack elasticity prediction best practice
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
Function (basic: parameter, return value)
How can CIOs use business analysis to build business value?
计组笔记(1)——校验码、原补码乘除计算、浮点数计算
2022-2028 global and Chinese FPGA prototype system Market Research Report
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
解密函数计算异步任务能力之「任务的状态及生命周期管理」
随机推荐
官宣!第三届云原生编程挑战赛正式启动!
Introduction to RT thread kernel (5) -- memory management
Function (basic: parameter, return value)
[thingsboard] how to replace the homepage logo
Live broadcast preview | container service ack elasticity prediction best practice
All in one 1413: determine base
A solution to the problem that variables cannot change dynamically when debugging in keil5
可观测|时序数据降采样在Prometheus实践复盘
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
Introduction to RT thread kernel (4) -- clock management
Debug insights
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
蛇形矩阵
Network security - record web vulnerability fixes
How should programmers learn mathematics
托管式服务网络:云原生时代的应用体系架构进化
自动语音识别(ASR)研究综述
Uncover the seven quirky brain circuits necessary for technology leaders
Machine learning -- neural network
CSDN body auto generate directory