acm专题---最小生成树
kruscal(eloge):
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1102
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.
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.
0 990 692
990 0 179
692 179 0
1
1 2
#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专题---最小生成树的更多相关文章
- acm专题---拓扑排序+优先队列
struct node{ int id; int cnt; node(int _id,int _cnt):id(_id),cnt(_cnt){} bool operator<(node a) c ...
- acm专题---最短路
spfa的时间复杂度是0(e) 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅 ...
- acm专题---KMP模板
KMP的子串长n,模式串长m,复杂度o(m+n),朴素做法的复杂度o((n-m+1)*m) 觉得大话数据结果上面这个讲得特别好 改进版本的KMP leetcode 28. Implement strS ...
- acm专题--并查集
题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...
- acm专题---dfs+bfs
题目来源:http://hihocoder.com/problemset/problem/1049 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...
- acm专题---动态规划
题目来源:http://hihocoder.com/problemset/problem/1400?sid=983096 #1400 : Composition 时间限制:10000ms 单点时限:1 ...
- acm专题---键树
题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- [kuangbin带你飞]专题六 最小生成树
学习最小生成树已经有一段时间了 做一些比较简单的题还算得心应手..花了三天的时间做完了kuangbin的专题 写一个题解出来记录一下(虽然几乎都是模板题) 做完的感想:有很多地方都要注意 n == 1 ...
随机推荐
- a++ 和 ++a 的区别
a++ 和 ++a 的区别 1)首先说左值和右值的定义: 变量和文字常量都有存储区,并且有相关的类型.区别在于变量是可寻址的(addressable)对于每一个变量都有两个值与其相联: ...
- Android Native jni 编程 Android.mk 文件编写
LOCAL_PATH 必须位于Android.mk文件的最开始.它是用来定位源文件的位置,$(call my-dir)的作用就是返回当前目录的路径. LOCAL_PATH := $(call my-d ...
- 【BZOJ 1129】[POI2008]Per 二叉堆
这个东西读完题之后,就能知道我们要逐位计算贡献.推一下式子,会发现,这一位的贡献,是当前剩余的数字形成的序列的总数,乘上所剩数字中小于s上这一位的数的个数与所剩数字的总数的比.所以我们维护“当前剩余的 ...
- SVN Server 500 NotLicensed 错误的解决方法
SVN Server 500 NotLicensed 错误的HTML页面显示 Not licensed The server encountered an internal error or misc ...
- eclipse中配置jbpm3.2插件
1.什么是jbpm?为什么要使用jbpm呢? 通俗一点讲,jbpm是一个负责管理工作流的一个产品,那么什么是工作流呢,所谓的工作流就是在办公自动化系统中,提交申请,申请经过多个部门领导审批,完成该流程 ...
- (转)IO复用,AIO,BIO,NIO,同步,异步,阻塞和非阻塞 区别
本文来自:https://www.cnblogs.com/aspirant/p/6877350.html?utm_source=itdadao&utm_medium=referral,非常感谢 ...
- 前端PHP入门-004-数据类型,特别需要注意字符串
人类世界对万事万物都有种类划分,例如: 哺乳动物 人.猫.马.鸭嘴兽-.等等 蔬菜 西红柿.波菜.茄子-.等等 水果 西瓜.桃子.苹果-.等等 数据类型:就是对数据分类的一个划分而已 整型就是整数 我 ...
- 跟我一起写Makefile(四)
书写命令———— 每条规则中的命令和操作系统Shell的命令行是一致的.make会一按顺序一条一条的执行命令,每条命令的开头必须以[Tab]键开头,除非,命令是紧跟在依赖规则后面的分号后的.在命令行之 ...
- 排序构造 GYM 101149 F - The Weakest Sith
题目链接:http://codeforces.com/gym/101149/my 题目大意:给你n个人,他们有成绩a,b,c.一个人如果两门课比另外一个人高,那么这个人就比那个人厉害.问,是否存在一个 ...
- 2015/10/9 Python核编初级部分学习总结
终于在十一长假之后的两天看完了<Python核心编程>的初级部分.虽然到后来两章,类和环境看得越来越慢,越来越难以理解.很多东西只能靠强记,也没办法真正掌握了,我想了想,还是不强迫自己去背 ...