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. (三)MySQL学习笔记

    [Leecode]175. 组合两个表 解答:由于是组合两个表的信息,很容易想到连接查询,这里使用左连接 select p.Firstname,p.Lastname,q.City,q.State fr ...

  2. 51nod 1277字符串中的最大值(拓展kmp)

    题意: 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd. 给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值.   题解 ...

  3. VSS2005设置不输入密码直接登录VSS

    1.登录管理员 2.Tools-->Options-->General -->Use network name for automatic user log in  去掉勾选不自动登 ...

  4. [洛谷P5136]sequence

    题目大意:有$T(T\leqslant10^5)$组询问,每次求$A_n(n\leqslant10^{18})$:$$A_n=\left\lceil\left(\dfrac{\sqrt5+1}2\ri ...

  5. [学习笔记]Min-25筛

    %%yyb %%zsy 一. 基本操作:筛1~N中的素数个数.n=1e9 设F(M,j)表示,2~M的所有数中,满足以下条件之一的数的个数:①x是质数②x最小质因子大于(注意是大于没有等号)$P_j$ ...

  6. Howto run google-chrome as root

    Just want to add a permanent solution to the problem: 1. Open google-chrome located in /usr/bin with ...

  7. 学习 opencv---(12)OpenCV 图像金字塔:高斯金字塔,拉普拉斯金字塔与图片尺寸缩放

    在这篇文章里,我们一起学习下 图像金字塔 的一些基本概念,如何使用OpenCV函数pyrUp和pyrDown 对图像进行向上和向下采样,以及了解专门用于缩放图像尺寸的resize函数的用法.此博文一共 ...

  8. Filter 介绍

    Filter 可认为是 Servlet 的一种加强版,它主要用于对用户请求进行预处理,也可以对 HttpServletResponse 进行后处理,是个典型的处理链. Filter 也可对用户请求生成 ...

  9. 000. 规范类的设计(ing)

    1.变量命名规范 变量命名有许多约定俗成的规范,下面的这些规范能有效提高程序的可读性: 标识符要能体现实际含义(顾名思义). 变量名一般用小写字母,如index,不要使用Index或INDEX. 用户 ...

  10. 关于java 获取 html select标签 下拉框 option 文本内容 隐藏域

    在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"typ ...