【kruscal】【最小生成树】poj2421 Constructing Roads
SB题,求最小生成树,其中有些边已经给您建好啦。
随意暴力即可。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int rank[],fa[],n,m,a[][],q,x,y,f1,f2,ans;
void init(){for(int i=;i<=n;i++) fa[i]=i;}
int findroot(int x)
{
if(fa[x]==x) return x;
int rt=findroot(fa[x]);
fa[x]=rt;
return rt;
}
void Union(int U,int V)
{
if(rank[U]<rank[V]) fa[U]=V;
else
{
fa[V]=U;
if(rank[U]==rank[V]) rank[U]++;
}
}
struct Edge{int u,v,w;Edge(const int &a,const int &b,const int &c){u=a;v=b;w=c;}Edge(){}};
bool cmp(const Edge &a,const Edge &b){return a.w<b.w;}
Edge edges[];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
edges[++m]=Edge(i,j,a[i][j]);
sort(edges+,edges+m+,cmp);
scanf("%d",&q);
init();
for(int i=;i<=q;i++)
{
scanf("%d%d",&x,&y);
f1=findroot(x); f2=findroot(y);
if(f1!=f2) Union(f1,f2);
}
for(int i=;i<=m;i++)
{
f1=findroot(edges[i].u); f2=findroot(edges[i].v);
if(f1!=f2)
{
Union(f1,f2);
ans+=edges[i].w;
}
}
printf("%d\n",ans);
return ;
}
【kruscal】【最小生成树】poj2421 Constructing Roads的更多相关文章
- HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏
Constructing Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- POJ2421 Constructing Roads
Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...
- POJ2421 Constructing Roads【最小生成树】
题意: 有N个点,有些点已经连接了,然后求出所有点的连接的最短路径是多少. 思路: 最小生成树的变形,有的点已经连接了,就直接把他们的权值赋为0,一样的就做最小生成树. 代码: prime: #inc ...
- POJ2421 Constructing Roads 最小生成树
修路 时限: 2000MS 内存限制: 65536K 提交总数: 31810 接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...
- 最小生成树 D - Constructing Roads
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- (最小生成树)Constructing Roads -- poj -- 2421
链接: http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2113 ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
随机推荐
- JQuery选择器$()的工作原理浅析
每次申明一个jQuery对象的时候,返回的是jQuery.prototype.init对象,很多人就会不明白,init明明是jQuery.fn的方法啊,实际上这里不是方法,而是init的构造函数,因为 ...
- size用法小记
By francis_hao Feb 14,2017 列出二进制文件各个段的大小和总大小 概述 选项解释 -A -B --format=compatibility 选择显示的格式, -A = - ...
- gcc用法小记
By francis_hao Feb 13,2017 概要 这里只列出了最常用的选项 选项解释 -c|-S|-E 启动gcc编译器时,它会顺序执行预处理.编译.汇编和连接(四个阶段的详细介绍 ...
- codeforces803D. Magazine Ad
D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...
- bzoj 2426 【HAOI2010】工程选址 贪心
[HAOI2010]工厂选址 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 447 Solved: 308[Submit][Status][Disc ...
- rest service技术选型
MySql workbench下载 http://dev.mysql.com/downloads/workbench/ 最好的8个 Java RESTful 框架 http://colobu.com/ ...
- 【BZOJ2326】【HNOI2011】数学作业 [矩阵乘法][DP]
数学作业 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input 输入文件只有一行为用空 ...
- [POJ1113&POJ1696]凸包卷包裹算法和Graham扫描法应用各一例
凸包的算法比较形象好理解 代码写起来也比较短 所以考前看一遍应该就没什么问题了..>_< POJ1113 刚开始并没有理解为什么要用凸包,心想如果贴着城堡走不是更好吗? 突然发现题目中有要 ...
- Basic-Paxos原理
Basic-Paxos //参考paxos made sample 核心是一致性协议算法. 问题 假定一个集合中的提案者可以产生一项决议. 一致性算法保证在提案者中只有一个提案可以形成决议. 如果没有 ...
- 利用php设置url转发 - 解决空间不提供子目录绑定功能的问题
由于很多新手都是使用的虚拟空间都是最便宜的那种,这空间一般不支持子目录绑定.但是很多朋友又想设置几个不同的二级域名访问不同的网站程序.于是大家找到了域名url转发,但是由于国家政策的原因,许多服务商暂 ...