题目链接: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
 Simple Output
179
 #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)的更多相关文章

  1. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  2. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  4. HDU1102 Constructing Roads —— 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...

  5. POJ - 2421 Constructing Roads (最小生成树)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  6. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  7. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

    题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...

  8. POJ2421 Constructing Roads 最小生成树

    修路 时限: 2000MS   内存限制: 65536K 提交总数: 31810   接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...

  9. HDU 1102 Constructing Roads(最小生成树,基础题)

    注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...

  10. HDU1102(最小生成树Kruskal)

    开学第三周.........真快尼 没有计划的生活真的会误入歧途anytime 表示不开心不开心不开心 每天都觉得自己的生活很忙 又觉得想做的事又没有完成 这学期本来计划重点好好学算法,打码码,臭臭美 ...

随机推荐

  1. 2018谷歌I/O开发者大会8大看点汇总 新品有哪些

    2018谷歌I/O开发者大会8大看点汇总 新品有哪些美国科技媒体The Verge近日撰文,列举了在即将召开的2018年谷歌I/O开发者大会上的8大看点,包括Android P.人工智能等等. 以下为 ...

  2. The word 'localhost' is not correctly spelled 这个问题怎么解决

    The word 'localhost' is not correctly spelled 这个问题怎么解决 有时工程中有下划线并提示 The word is not correctly spelle ...

  3. nodejs之glob与globby

    glob glob允许使用规则,从而获取对应规则匹配的文件.这个glob工具基于javascript.它使用了 minimatch 库来进行匹配 安装 npm install glob 引入 cons ...

  4. linux目录详解

    网卡的配置文件目录 /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0                                     ...

  5. Dubbo常用配置文件分析及核心源码阅读(SPI.Extension)

    1.多版本支持: 基于上篇博客的 快速启动 Dubbo 服务 的代码进行多版本支持的演示:基于原来的实现类GpHelloImpl ,我们需要新增一个新版本的实类:GpHelloImpl2 public ...

  6. PHP编译安装时常见错误解决办法

    转载自:http://www.bkjia.com/PHPjc/1008013.html This article is post on https://coderwall.com/p/ggmpfa c ...

  7. ajax之全局函数

    1.全局函数:$.each(array,function(){1,value}),通过$/jQuery直接调用 对象函数:$("#name").val(); jQuery UI:$ ...

  8. 华为设备默认console密码

    admin@huawei.com Admin@huawei.com Admin@huawei huawei.com huawei@123 huawei Change_Me

  9. vs无法启动程序,操作在当前状态中是非法的

    问题的图片: 解决方案: 工具--选项--调试--常规--启用asp.net的JavaScript调试(chrome和ie)去掉勾选

  10. WCF 非http寄宿IIS

    摘要 从IIS 7 开始, IIS增加了对非HTTP协议的支持. 因此, 自IIS 7之后, 可以将NetTcpBinding等非HTTP协议的Bindings直接寄宿在IIS上面. 本文将介绍如何在 ...