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. [BZOJ1503][NOI2004]郁闷的出纳员 无旋Treap

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MB Description OIER公司是一家大型专业化软件公司,有着数以万计的员 ...

  2. DjangoORM使用mysql注意

    注意事项1:需要在project下的setting里面做设置.让Django生成MySQL类型的数据库. 注意事项2:在Django内部,连MySQL的时候,需要添加下面2句代码: 4.******* ...

  3. 【JavaScript】函数表达式

    一.前言        接着上一篇的内容,继续学习JavaScript. 二.内容       函数的声明 function functionName(arg0,arg1,arg2){ //函数体 } ...

  4. 【BZOJ2306】幸福路径(动态规划,倍增)

    [BZOJ2306]幸福路径(动态规划,倍增) 题面 BZOJ 题解 不要求确切的值,只需要逼近 显然可以通过移动\(\infty\)步来达到逼近的效果 考虑每次的一步怎么移动 设\(f[i][j]\ ...

  5. 洛谷 P3768 简单的数学题 解题报告

    P3768 简单的数学题 题目描述 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数\(n\)和一个整数\(p,\)你需要求出\((\sum_{i=1}^n\sum_{j=1}^n ijgc ...

  6. bzoj2213: [Poi2011]Difference(思维题)

       今天颓了一天T T 这题有两种写法... ①预处理出每种字符在原字符串中的位置,枚举两种字符作为最大值和最小值,把这两种字符的坐标归并排序,把最大值设为1,最小值设为-1,求最大子段和.注意因为 ...

  7. Python3 字典 clear()方法

     Python3 字典 描述 Python 字典 clear() 函数用于删除字典内所有元素. 语法 clear()方法语法: dict.clear() 参数 NA. 返回值 该函数没有任何返回值. ...

  8. Spring MVC @RequestParam

    案例来说明 @RequestMapping("user/add") public String add(@RequestParam("name") String ...

  9. move_base的全局路径规划代码研究

    algorithmn parameter code 主要是以下三个函数 计算所有的可行点 怎么计算一个点的可行点 从可行点中计算路径path todo algorithmn 算法的解释 Dijkstr ...

  10. CentOS7,安装Tomcat8.5、JDK1.8,并设置开机启动(Linux CentOS Tomcat、JDK+Tomcat、Tomcat开机自启动)

    1.下载JDK1.8.Tomcat8 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...