题目链接:

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

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27178    Accepted Submission(s): 10340

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
 
Sample Output
179
 
Source
分析:
裸最小生成树
注意点:
自己到自己的距离初始化为无穷大
多组输入(这个题目没有说,但是需要才能ac)
存图的时候要多注意,要细心,不能存错了,尤其是下标
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000
#define max_v 105
int g[max_v][max_v];//g[i][j] 表示i点到j点的距离
int n,sum;
void init()
{
for(int i=; i<n; i++)
for(int j=; j<n; j++)
g[i][j]=INF;
}
void prim()
{
// int close[n];//记录不在s中的点在s中的最近邻接点
int lowcost[n];//记录不在s中的点到s的最短距离,即到最近邻接点的权值
int used[n];//点在s中为1,否则为0
for(int i=; i<n; i++)
{
//初始化,s中只有一个点(0)
lowcost[i]=g[][i];//获取其他点到0点的距离,不相邻的点距离无穷大
// close[i]=0;//初始化所有点的最近邻接点都为0点
used[i]=;//初始化所有点都没有被访问过
}
used[]=;
for(int i=; i<n; i++)
{
//找点
int j=;
for(int k=; k<n; k++) //找到没有用过的且到s距离最小的点
{
if(!used[k]&&lowcost[k]<lowcost[j])
j=k;
}
// printf("%d %d %d\n",close[j]+1,j+1,lowcost[j]);
sum+=lowcost[j];
used[j]=;//j点加入到s中
//松弛
for(int k=; k<n; k++)
{
if(!used[k]&&g[j][k]<lowcost[k])
{
lowcost[k]=g[j][k];
// close[k]=j;
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
sum=;
init();
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
int x;
scanf("%d",&x);
if(i==j)
continue;
g[i][j]=x;
}
}
int q;
scanf("%d",&q);
for(int i=; i<q; i++)
{
int a,b;
scanf("%d %d",&a,&b);
g[a-][b-]=;
g[b-][a-]=;
}
prim();
printf("%d\n",sum);
}
return ;
}

HDU 1102(Constructing Roads)(最小生成树之prim算法)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  6. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  8. hdu 1102 Constructing Roads(kruskal || prim)

    求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...

  9. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

随机推荐

  1. HDU 2553(N皇后)(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn. ...

  2. DOM基础操作(三)

    DOM剩余的两个操作一并带来! 1.删除操作 removeChild 这个方法依然是父级调用的,参数就是要删除的子节点,其实实际上是剪切,这个方法会把我们删除掉的元素给返回,我们可以用一个变量去保存这 ...

  3. 苹果ios,下拉菜单错位的问题(目前iphone x没发现有这个问题)

    苹果手机,点击下拉框,再点击确认按钮,页面位置错乱(感觉背景整体往上移动了一段距离,并且,页面所有的元素都往上移了一定的距离),导致手机页面底部留白的问题,并且,元素实际位置跟页面位置不一致. 解决方 ...

  4. Python实现冒泡,选择排序

    def bubble(num): for i in range(len(num)-1): for j in range(len(num)-i-1): if(num[j]>num[j+1]): t ...

  5. 中国国内 - 可用API合集

    中国国内 - 可用API合集 收录一篇中国国内可用API合集,分享给大家 目录 笔记 出行 词典 电商 地图 电影 即时通讯 开发者网站 快递查询 旅游 社交 视频 天气 团队协作 图片与图像处理 外 ...

  6. Python爬虫教程-08-post介绍(百度翻译)(下)

    Python爬虫教程-08-post介绍(下) 为了更多的设置请求信息,单纯的通过urlopen已经不太能满足需求,此时需要使用request.Request类 构造Request 实例 req = ...

  7. 新建虚拟机,每次都提示无法连接虚拟设备 ide1:0

    处理方式:看到了这个老哥http://www.cnblogs.com/dean-du/p/6888513.html的博客,发现问题是一样的,所以记录一下. 将虚拟机设置中的CD/DVD选项中的连接更改 ...

  8. mac安装软件提示没有权限

    Mac 安装软件基本是各种爽,自动更新啥. 但是有一种提示没有权限的错误,很不爽,还要sudo管理员权限 有一个修复 /usr/local目录权限的命令 sudo chown -R 'whoami' ...

  9. springDataJpa学习笔记

    目录 前言 springData 准备 引用 xml配置初始化JPA pojo dao层接口 使用 新增.修改:save 删除 查询所有 根据ID查询findById 命名规则查询(条件查询) 自定义 ...

  10. centos 7.2 Apache+mysql+php step by step备忘

    1. 如何允许laravel程序执行sudo shell脚本? chmod u+w /etc/sudoers ; echo "apache ALL=(ALL) NOPASSWD:ALL&qu ...