当前位置:网站首页>(7/29) Basic board minimum spanning tree prim+kruskal
(7/29) Basic board minimum spanning tree prim+kruskal
2022-07-30 21:07:00 【Salted egg_dd】
1.prim algorithm to find the minimum spanning tree
Given an undirected graph with nn points and mm edges, there may be multiple edges and self-loops in the graph, and the edge weights may be negative.
Find the sum of the tree edge weights of the minimum spanning tree. If the minimum spanning tree does not exist, output
impossible
.Given an undirected graph G=(V,E)G=(V,E), where VV represents the set of points in the graph, EE represents the set of edges in the graph, n=|V|n=|V|, m=|E|m=|E|.
The undirected connected subgraph composed of all nn vertices in VV and n−1n−1 edges in EE is called a spanning tree of GG, and the spanning tree with the smallest sum of edge weights iscalled the minimum spanning tree of the undirected graph GG.
Input format
The first line contains two integers nn and mm.
The next mm lines, each line contains three integers u,v,wu,v,w, indicating that there is an edge with weight ww between point uu and point vv.
Output format
A total of one line, if there is a minimum spanning tree, output an integer, indicating the sum of the tree edge weights of the minimum spanning tree, if the minimum spanning tree does not exist, output
impossible
.Data Range
1≤n≤5001≤n≤500,
1≤m≤1051≤m≤105,
The absolute value of the edge weights involved in the graph does not exceed 1000010000.Sample input:
4 51 2 11 3 21 4 32 3 23 4 4
Sample output:
6
#include using namespace std;const int N=100010;int res=0;int mp[510][510];bool st[510];int dis[510];int n,m;const int inf=0x3f3f3f3f;int prim(){for(int i=0;imp[t][j])dis[j]=mp[t][j];}}return res;}int main(){int a,b,c;scanf("%d %d",&n,&m);memset(dis,inf,sizeof(dis));memset(mp,inf,sizeof(mp));while(m--){scanf("%d %d %d",&a,&b,&c);mp[a][b]=mp[b][a]=min(mp[a][b],c);}int t=prim();if(t==inf)printf("impossible\n");elseprintf("%d\n",t);return 0;}
2.kruskal algorithm
Given an undirected graph with nn points and mm edges, there may be multiple edges and self-loops in the graph, and the edge weights may be negative.
Find the sum of the tree edge weights of the minimum spanning tree. If the minimum spanning tree does not exist, output
impossible
.Given an undirected graph G=(V,E)G=(V,E), where VV represents the set of points in the graph, EE represents the set of edges in the graph, n=|V|n=|V|, m=|E|m=|E|.
The undirected connected subgraph composed of all nn vertices in VV and n−1n−1 edges in EE is called a spanning tree of GG, and the spanning tree with the smallest sum of edge weights iscalled the minimum spanning tree of the undirected graph GG.
Input format
The first line contains two integers nn and mm.
The next mm lines, each line contains three integers u,v,wu,v,w, indicating that there is an edge with weight ww between point uu and point vv.
Output format
A total of one line, if there is a minimum spanning tree, output an integer, indicating the sum of the tree edge weights of the minimum spanning tree, if the minimum spanning tree does not exist, output
impossible
.Data Range
1≤n≤1051≤n≤105,
1≤m≤2∗1051≤m≤2∗105,
The absolute value of the edge weights involved in the graph does not exceed 10001000.Sample input:
4 51 2 11 3 21 4 32 3 23 4 4
Sample output:
6
Basic idea: first sort all edge weights according to their weights (stored in a structure), then enumerate each edge, use the union check set, if the two points are not in a connected block, thenAdd this edge to the set, and put the two points in the same connected block,
#include using namespace std;const int N=200010;int p[N];struct node{int st,ed,w;}a[N];int Find(int x){if(p[x]!=x)p[x]=Find(p[x]);return p[x];}bool cmp(struct node x,struct node y){return x.w
边栏推荐
猜你喜欢
MySQL_关于JSON数据的查询
IDEA2018.3.5取消双击Shift快捷键
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
MySQL ODBC驱动简介
MySQL60 homework
[Deep Learning] Target Detection | SSD Principle and Implementation
canvas基础讲解加示例
Image Restoration by Estimating Frequency Distribution of Local Patches
《快速掌握QML》第六章 动画
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
随机推荐
mysql deadlock
【luogu P8031】Kućice(计算几何)
What is the common factor
MySQL Workbench 安装及使用
使用map函数,对list中的每个元素进行操作 好像不用map
Swift RegexBuilder Vs. Raku Grammar
关于SFML Rect.inl文件报错的问题
JDBC(详解)
HJ85 longest palindrome substring
ELF:加载过程
tcp协议传输中的粘包问题
【深度学习】目标检测|SSD原理与实现
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNsfor Soft Decoding of JPEG-Compressed Images
我是如何让公司后台管理系统焕然一新的(上) -性能优化
ENS emoji domain name is on fire!Hype or opportunity?
Mysql——字符串函数
Activiti 工作流引擎 详解
[Deep Learning] Target Detection | SSD Principle and Implementation
R package调试
牛客小白月赛53 A-E