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.

 
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.

 
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
#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 */

  

acm专题---最小生成树的更多相关文章

  1. acm专题---拓扑排序+优先队列

    struct node{ int id; int cnt; node(int _id,int _cnt):id(_id),cnt(_cnt){} bool operator<(node a) c ...

  2. acm专题---最短路

    spfa的时间复杂度是0(e) 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅 ...

  3. acm专题---KMP模板

    KMP的子串长n,模式串长m,复杂度o(m+n),朴素做法的复杂度o((n-m+1)*m) 觉得大话数据结果上面这个讲得特别好 改进版本的KMP leetcode 28. Implement strS ...

  4. acm专题--并查集

    题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...

  5. acm专题---dfs+bfs

    题目来源:http://hihocoder.com/problemset/problem/1049 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...

  6. acm专题---动态规划

    题目来源:http://hihocoder.com/problemset/problem/1400?sid=983096 #1400 : Composition 时间限制:10000ms 单点时限:1 ...

  7. acm专题---键树

    题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...

  8. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树

    题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...

  9. [kuangbin带你飞]专题六 最小生成树

    学习最小生成树已经有一段时间了 做一些比较简单的题还算得心应手..花了三天的时间做完了kuangbin的专题 写一个题解出来记录一下(虽然几乎都是模板题) 做完的感想:有很多地方都要注意 n == 1 ...

随机推荐

  1. 【BZOJ3166】ALO(主席树)

    [BZOJ3166]ALO(主席树) 题面 权限题qwq 资磁洛谷 题解 用一个\(set\)求出左右侧比这个数大的第\(2\)个数, 然后用可持久化\(Trie\)算一下就好啦 #include&l ...

  2. 洛谷 P1013 进制位 【搜索 + 进制运算】

    题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: + L K V E L L K V E K K V E KL V V E KL KK E E ...

  3. BZOJ 2959: 长跑 解题报告

    2959: 长跑 Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能"为祖国健康工作五十年",同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑 ...

  4. 实现了一下Berlekamp-Massey

    //from https://www.cnblogs.com/TSHugh/p/9265155.html //在FP中求固定项数数列的线性递推式 //此递推式严格符合数学定义,故可能在末尾出现一些看起 ...

  5. 【线段树】【CF19D】 Points

    传送门 Description 在一个笛卡尔坐标系中,定义三种操作: \(add(x,y)\),将点\((x,y)\)标记在坐标系上 \(find(x,y)\),查询点\((x,y)\)严格右上方中, ...

  6. Android Studio常用的快捷键

    罗列一些常用的快捷键 全局快捷键(比较重要的)   ALT + ENTER 工程快速修复 CTRL + SHIFT + A 快速查找 CTRL + ALT + L (Win) 格式化代码(我的锁屏的快 ...

  7. [vim]乱码问题

    在vim输入中文乱码 1. 检查系统是否支持中文 locale -a 没有中文支持 安装中文包 apt-get install language-pack-zh-hans -y 2.这样可以输入中文了 ...

  8. python自学笔记(一)

    我没学过python,通过网上和一些图书资料,自学并且记下笔记. 很多细节留作以后自己做项目时再研究,这样能更高效一些. python基础自学笔记 一.基本输入和输出 pthon3.0用input提示 ...

  9. Hadoop YARN 的工作流程简述

    1.Client 向 YARN 提交应用程序,其中包括 ApplicationMaster 程序及启动 ApplicationMaster 命令2.ResourceManager 为该 Applica ...

  10. [DeeplearningAI笔记]卷积神经网络1.4-1.5Padding与卷积步长

    4.1卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.4Padding 一张\(6*6\)大小的图片,使用\(3*3\)的卷积核设定步长为1,经过卷积操作后得到一个\(4*4 ...