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 ...
随机推荐
- 洛谷 P4754 True Vegetable 解题报告
P4754 True Vegetable 题目描述 小A现在有N道题,编号为1,2,⋯,N.每道题的起始毒瘤程度为0或1.在每天,小A可以将编号连续的K道题的毒瘤程度+1.但小B因为本身比较菜,不是很 ...
- SCOI2014极水的题解- -
话说SCOI都考了1个月了,终于拿出决心把题解补完了,但都说了是极水的题解,大家就看着玩吧- - DAY1 T1:目标是找最长不降子序列,先就有一个比较显然的结论,就是假如我们要拔高区间[L, R], ...
- 表格隔行变色_CSS实现鼠标悬停高亮
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- bzoj4569: [Scoi2016]萌萌哒(ST表+并查集)
好喵喵的题 将一个要求用ST表分割成logn个要求,如果把f[i][j]和f[u][v]在同一个集合,那么f[i][j-1]和f[u][v-1],f[i+2^(j-1)][j-1]和f[u][u+2^ ...
- pxp
Time Limit: 2000 ms Memory Limit: 512 MB Description 给定 \(n\), 求\(\sum\limits_{p,q∈primes}[pq≤n]\) ( ...
- selenium - javascript - 滚动条
虽然WebDriver提供了操作浏览器的前进和后退方法,但对于浏览器滚动条并没有提供相应的操作方法.在这种情况下,就可以借助JavaScript来控制浏览器的滚动条.WebDriver提供了execu ...
- 【计算机视觉】SIFT中LoG和DoG比较
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 在实际计算时,三种方法计算的金字塔组数noctaves ...
- 「Django」rest_framework学习系列-版本认证
1.自己写: class UserView(APIView): versioning_class = ParamVersion def get(self,request,*args,**kwargs) ...
- UVA 818 Cutting Chains
https://vjudge.net/problem/UVA-818 题意: 有n个圆环,其中有一些已经扣在了一起.现在需要打开尽量少的圆环,使得所有圆环可以组成一条链 n<=15 因为n< ...
- 非法字符:"\ufeff"
Eclipse项目导入IDEA可能遇到这样的问题 ,原因就是: 带BOM的UTF-8」和「无BOM的UTF-8」 方法一.用Notepad++把文件转成无BOM的UTF-8 另存为,替换原来的文件 方 ...