题目连接: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): 21947    Accepted Submission(s):
8448

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
 
题目大意:有N个村庄,现在需要修一些路,使得任意两个村庄之间可以连通(可以间接相连),给出每条路的花费,求出最少需要的花费。已知一些路已经修好了。
 
解题思路:根据题目的输入数据,可以选用以顶点为主导的Prim算法求MST。
 
AC代码:
2017-03-02 20:21:34 Accepted 15MS 1456K 878 B G++
 1 #include <stdio.h>
#include <string.h>
#define inf 0x3f3f3f3f int map[][],vis[],dis[];
int n; void prim()
{
int i,j,pos,min,sum = ;
for (i = ; i <= n; i ++)
{
vis[i] = ;
dis[i] = map[][i];
}
vis[] = ; dis[] = ;
for (i = ; i < n; i ++)
{
pos = -; min = inf;
for (j = ; j <= n; j ++)
{
if (!vis[j] && min > dis[j])
{
min = dis[j];
pos = j;
}
}
vis[pos] = ;
sum += min;
for (j = ; j <= n; j ++)
if (!vis[j] && dis[j] > map[pos][j])
dis[j] = map[pos][j];
}
printf("%d\n",sum);
}
int main ()
{
int i,j,m,a,b;
while (~scanf("%d",&n))
{
for (i = ; i <= n; i ++)
for (j = ; j <= n; j ++)
scanf("%d",&map[i][j]);
scanf("%d",&m);
for (i = ; i < m; i ++)
{
scanf("%d%d",&a,&b);
map[a][b] = map[b][a] = ;
}
prim();
}
return ;
}

算法理解: http://www.cnblogs.com/yoke/p/6506492.html

hdu 1102 Constructing Roads (Prim算法)的更多相关文章

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

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

  2. HDU 1102(Constructing Roads)(最小生成树之prim算法)

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

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

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

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

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

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

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

  6. hdu 1102 Constructing Roads Kruscal

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

  7. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1102 Constructing Roads(kruskal)

    Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...

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

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

随机推荐

  1. linux 系统管理(三) 系统信息查看 inx

    inxi infomation x i (信息每个人都爱) Inxi 最初是为控制台和 IRC(网络中继聊天)开发的一个强大且优秀的命令行系统信息脚本.可以使用它获取用户的硬件和系统信息,它也用于调试 ...

  2. 2.阿里实人认证 .net 准备工作2 转换demo

    1.引入阿里的SDK 2. 搬一下java 的代码 DefaultProfile profile = DefaultProfile.GetProfile( "cn-hangzhou" ...

  3. 分分钟钟学会Python -基础&运算符

    day002 基础&运算符 1.循环语句 ### 1.循环格式 while 条件: print('') ''' while True: print('人生苦短,我用Python.') ''' ...

  4. Windows下代替自带cmd的开源软件cmder

    cmder cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令 下载 Cmder官网 下载的时候,会有两个版本,分别是mini与 ...

  5. python计算π及进度条显示

    今天老师布置了一个课后作业,去尽可能的准确计算π的值,还要显示时间和进度条,对于python小白的我,当然是综合书上和网上的知识,自己做了一个小程序,代码如下: 一.写代码的准备工作:用pip下载第三 ...

  6. mysql安装启动 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    首次安装mysql 启动 mysql -uroot 以下错误: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using ...

  7. vue-router学习

    JS push goTo(){ , postId: ' }}) } router.js // 动态路径参数 以冒号开头 { path: '/user/:id', component: User } { ...

  8. Java - 关于子类父类的向上转型,向下转型

    以下Animal为父类,Dog,Cat作为子类,分别继承Animal class Animal{ public void shout(){ System.out.println("叫了一声& ...

  9. 读取obj文件用Mesh创建实例化

    using UnityEngine; using System.Collections; using System.IO; using System.Collections.Generic; usin ...

  10. 什么是DDL,DCL,DML

    1.DCL 数据库控制语言,全称是Data Control Language 关键字类型:grant.remove 2.DDL 数据库定义语言,全称是Data Definition Language ...