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 表示不开心不开心不开心 每天都觉得自己的生活很忙 又觉得想做的事又没有完成 这学期本来计划重点好好学算法,打码码,臭臭美 ...
随机推荐
- PID控制器开发笔记之十一:专家PID控制器的实现
前面我们讨论了经典的数字PID控制算法及其常见的改进与补偿算法,基本已经覆盖了无模型和简单模型PID控制经典算法的大部.再接下来的我们将讨论智能PID控制,智能PID控制不同于常规意义下的智能控制,是 ...
- 处理:“ORA-28002: the password will expire within 7 days”的问题
一:问题描述: 二:处理步骤 [oracle@localhost 2018_07_14]$ rlwrap sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3 ...
- Confluence 6 系统运行信息中的 JVM 内存使用情况
当前一个正在运行的 Confluence 6 实例的内存使用情况 https://www.cwiki.us/display/CONF6ZH/Viewing+System+Information
- git码云上传本地项目
可参考:https://blog.csdn.net/huangfei711/article/details/69388230 .在你的项目上鼠标右击点击Git bash git config --gl ...
- 如何在cmd中安装python第三方模块
打开 cmd终端 1 输入pip install 模块名. 2 等待安装完成即可.
- spring boot 整合 shiro
shrio官网:https://shiro.apache.org/ Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理.借助Shiro易于理解的A ...
- LeetCode(95): 不同的二叉搜索树 II
Medium! 题目描述: 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], ...
- LeetCode(3):无重复字符的最大子串
本内容是LeetCode第三道题目:无重复字符的最大子串 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 20:14: ...
- CSS----注释的坑
css 中 style 注释 需要用 /* */ 第一种方法注释,结果是不正确的,css布局会出现问题 第二种方式注释正确,布局不会出现问题
- spfa+01 规划
尼玛的哪里错了.. /* 在有向图上找一个环,使结点权值和/边权和的比例值最大 01规划,设比例为l,那么将每条边的权值改成a[u]-l*w,如果有正权环,则比例l可行 如何判图中存在正权环?将 权值 ...