Constructing Roads-最小生成树(kruskal)
题目链接: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.
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.
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.
the length of all the roads to be built such that all the villages are
connected, and this value is minimum.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; struct node
{
int u,v,cost;
}a[];
int pre[];
int fin(int x)
{
if(x==pre[x])
{
return x;
}
else
{
return pre[x]=fin(pre[x]);
}
} void join(int x,int y)
{
int t1=fin(x);
int t2=fin(y);
if(t1!=t2)
{
pre[t1]=t2;
}
} bool cmp(node x,node y)
{
return x.cost<y.cost;
} int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
{
pre[i]=i;
}
int num,cnt=;;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&num);
a[cnt].u=i;
a[cnt].v=j;
a[cnt].cost=num;
cnt++;
}
}
sort(a,a+cnt,cmp);
int sum1=,sum=;//此处算是一个小剪枝吧
int q;
scanf("%d",&q);
int c,d;
for(int i=;i<q;i++)
{
scanf("%d%d",&c,&d);
if(fin(c)!=fin(d))
{
join(c,d);//已经修好路的村庄链接成一个集合
sum1++;
}
}
for(int i=;i<cnt;i++)
{
if(fin(a[i].u)!=fin(a[i].v))
{
join(a[i].u,a[i].v);
sum+=a[i].cost;
sum1++;
}
if(sum1==n-)
{
break;
}
}
printf("%d\n",sum);
}
return ;
}
Constructing Roads-最小生成树(kruskal)的更多相关文章
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- HDU1102 Constructing Roads —— 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
- POJ2421 Constructing Roads 最小生成树
修路 时限: 2000MS 内存限制: 65536K 提交总数: 31810 接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...
- HDU 1102 Constructing Roads(最小生成树,基础题)
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...
- HDU1102(最小生成树Kruskal)
开学第三周.........真快尼 没有计划的生活真的会误入歧途anytime 表示不开心不开心不开心 每天都觉得自己的生活很忙 又觉得想做的事又没有完成 这学期本来计划重点好好学算法,打码码,臭臭美 ...
随机推荐
- Confluence 6 数据库表-杂项(Miscellaneous)
这些部分是一些其他的表格,这些表格有必要在这里提及下能帮你更好的了解系统. os_propertyentry 有关实体和属性相关的特性. bandana 所有的持久层.这个表格包含的的内容有用户设置和 ...
- Confluence 6 附件存储选项
在早期的 Confluence 版本中,我们允许存储附件到 WebDav 或者 Confluence 数据库中.针对新的 Confluence 安装,我们不再支持这 2 种存储了. 本地文件系统 在默 ...
- Es6模块语法笔记
/** * Created by Administrator on 2017/4/15. */ /*---------------------export命令--------------------- ...
- asynicio模块以及爬虫应用asynicio模块(高性能爬虫)
一.背景知识 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,只用一个线程且采用串行的方式执行,那只能等待爬取一个结束后才能继续下一个,效率会非常低. 需要强调的是 ...
- yum安装软件内容
linux yum源改为阿里yum源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back ...
- LeetCode(89):格雷编码
Medium! 题目描述: 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以 0 开头. 例如,给定 ...
- 【python】内存相关
1. /proc/pid/status 可以查看进程相关的详细信息,当内存异常时可查看 参考:http://blog.csdn.net/beckdon/article/details/4849190 ...
- PDF编辑方法,PDF如何去除数字签名
有些人会在PDF文件中添加数字签名,但当PDF文件有数字签名的时候就无法对PDF文件进行编辑.添加等操作.这个时候就需要去除PDF文件中的数字签名了,要怎么做呢,就由我来跟大家分享一下小编我的去除数字 ...
- sqlmap+DWAV测试实战(一)
root@kali-yaming:~# sqlmap -u "http://172.18.225.39/vulnerabilities/sqli/?id=1&Submit=Submi ...
- 2017-2018-2 20165314 实验三《 敏捷开发与XP实践》实验报告
知识点: 1.XP团队使用现场客户.特殊计划方法和持续测试来提供快速的反馈和全面的交流: -XP是以开发符合客户需要的软件为目标而产生的一种方法论 -XP是一种以实践为基础的软件工程过程和思想 -XP ...