当前位置:网站首页>Finding minimum spanning tree by using union search set
Finding minimum spanning tree by using union search set
2022-06-21 14:50:00 【User 6978604】
What is? Kruskal Algorithm ?
Kruskal Algorithm is a method to find the minimum spanning tree , It is similar to greedy algorithm , First, sort the edges according to their weights from small to large , Then the smaller edges are added to the set of process solutions , The key is to solve the problem of the circuit , And we can solve this problem by using the union search set , That is, before each edge is added , Determine whether the two vertices of the edge are in the same set , Same, skip , Otherwise, it is added to the process solution , Repeat the process , Know that there is only one set at the end , The set is the desired .
subject :
originate Luogu
Title Description
As the title , Give an undirected graph , Find the minimum spanning tree , If the graph is not connected , The output orz.
Input format
The first line contains two integers N,M, Indicates that the graph has in common N A node and M Undirected edge .
Next M Each row contains three integers Xi, Yi, Zi, It means that there is a length of Zi The undirected edge connection node of Xi, Yi
Output format
If the graph is connected , Then output an integer representing the sum of the lengths of the edges of the minimum spanning tree . If the graph is not connected, the output is orz.
solve :
/*
* @Author: YaleXin
* @Date: 2020-01-21 11:11:04
* @LastEditTime: 2020-04-10 22:51:47
* @LastEditors: YaleXin
* @Description:
* @FilePath: \my_c_workspace\luo_gu\P3366.c
* @ Prayer does not appear BUG
*/
#include <stdio.h> // utilize kruskal Algorithm
int arc[200001][3], n, m; // arc The last of represents the weight , The first two represent adjacency points
int arc_index[200001]; // Number of storage side , After sorting, you get the number in ascending order according to the edge weight
int anc[200001]; // And look up the auxiliary array , Store the set to which each point belongs
int find_anc(int son) {
if (anc[son] == son)
return son;
else {
// Compress path
anc[son] = find_anc(anc[son]);
return anc[son];
}
}
void q_sort(int left, int right) {
if (left >= right) return;
int l = left, r = right, piv = arc_index[left];
while (l < r) {
while (l < r && arc[arc_index[r]][2] >= arc[piv][2]) r--;
arc_index[l] = arc_index[r];
while (l < r && arc[arc_index[l]][2] <= arc[piv][2]) l++;
arc_index[r] = arc_index[l];
}
arc_index[l] = piv;
q_sort(left, l - 1);
q_sort(r + 1, right);
}
int main() {
int i, sum = 0;
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) anc[i] = i; // At first each point is a separate set
for (i = 0; i < m; i++) {
scanf("%d %d %d", &arc[i][0], &arc[i][1], &arc[i][2]);
arc_index[i] = i;
}
q_sort(0, m - 1); // Sort the weights of the edges
for (i = 0; i < m; i++) { // The smallest edge in turn “ and ”
// If both belong to “ The ancestors ”, Then skip
if (find_anc(arc[arc_index[i]][1]) == find_anc(arc[arc_index[i]][0]))
continue;
// If different , It means that it conforms to , You can add
else {
anc[find_anc(anc[arc[arc_index[i]][0]])] = arc[arc_index[i]][1];
sum += arc[arc_index[i]][2];
}
}
printf("%d", sum);
return 0;
}
/*
4 5
1 2 2
1 3 8
1 4 3
2 3 4
3 4 3
*/边栏推荐
- Skills of assigning IP address by DHCP in secondary route after wireless bridge
- Kubeneters installation encountered gcr The IO image warehouse cannot be pulled
- Dplayer development barrage background
- There is a PPP protocol between routers. How can there be an ARP broadcast protocol
- Nmap scan port tool
- Chapter 5 - application layer
- Win10 installation and configuration mongodb
- Computer shortcuts During sorting, fill in as needed
- Redis introduction and Practice (with source code)
- Small case of throttling function
猜你喜欢

HSV color model and color component range in opencv

The code remotely calls aria2 to download URL resources or BT seeds

Chapter 3 - data link layer

Detailed explanation of dynamic planning

Qt-5-multi window programming

Nmap scan port tool

Promotion guide for large enterprises: material preparation, PPT writing and on-site defense
![Cool background shadow effect [second with layered feeling] [picture hover style]](/img/ca/d68f2cf9f9af7b9346032b2a6e120b.jpg)
Cool background shadow effect [second with layered feeling] [picture hover style]

Qt-6-file IO

Summary of the most basic methods of numpy
随机推荐
NPM package management configuration file [package.json and node\u modules configuration details and how to develop their own packages and publish them on NPM]
Go admin framework analysis (2-1)
C语言的指针
Nodejs process has too many open files
Mqtt keepalive and reconnect
2021-2022 recall of the final exam questions of non relational database of Shandong University
Decipher Bishengyuan: is it tea that consumers buy, or is it IQ tax?
Analysis of ROC and AUC
Define structure dynamically when macro is defined
Detailed explanation of dynamic planning
What is SQL injection
Redis introduction and Practice (with source code)
Summary of the most basic methods of numpy
Numpy: basic package for high performance scientific computing & data analysis
Dplayer development barrage background
Summary of web development technology knowledge
Common data processing of machine learning
Viewing tcp/ip network communication from the sending of an email
Fundamentals of C language 13: file input / output
There is a PPP protocol between routers. How can there be an ARP broadcast protocol