acm专题---最小生成树
kruscal(eloge):
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1102
Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <iostream> using namespace std; #include <vector> #include<algorithm> #include<queue> #include<string> #include<map> #include<math.h> #include<iomanip> #include<stack> #include<string.h> const int maxnum=101; int mymap[maxnum][maxnum]; int n; int fa[maxnum]; struct edge{ int point1; int point2; int weight; edge( int _point1, int _point2, int _weight) { point1=_point1; point2=_point2; weight=_weight; } }; int cmp(edge a,edge b) { return a.weight<b.weight; } int findfa( int x) { return fa[x]==x?x:(fa[x]=findfa(fa[x])); } void mergefa( int x, int y) { fa[findfa(x)]=findfa(fa[y]); } void kruscal() { vector<edge> edges; for ( int i=0;i<n;i++) { for ( int j=0;j<i;j++) { edges.push_back(edge(i,j,mymap[i][j])); } } sort(edges.begin(),edges.end(),cmp); int m=n*(n-1)/2; int cnt=0; int ans=0; for ( int i=0;i<m;i++) { int x1=edges[i].point1; int x2=edges[i].point2; int fa1=findfa(x1); int fa2=findfa(x2); if (fa1!=fa2) { mergefa(x1,x2); cnt+=1; ans+=edges[i].weight; if (cnt>=n-1) break ; } } cout<<ans<<endl; } int main() { while (cin>>n) { for ( int i=0;i<n;i++) { for ( int j=0;j<n;j++) { cin>>mymap[i][j]; } } for ( int i=0;i<=n;i++) fa[i]=i; int m; cin>>m; for ( int i=0;i<m;i++) { int x,y; cin>>x>>y; mymap[x-1][y-1]=mymap[y-1][x-1]=0; } kruscal(); } return 0; } /* Sample Input 3 0 990 692 990 0 179 692 179 0 1 1 2 Sample Output 179 */ |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)