题目连接: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环境部署安装Maven

    第一步:Maven下载 1. 手动下载 访问官网:http://maven.apache.org/download.cgi 当前最新版本是3.6.0,如果想下载其他版本 可通过点击下图选中项进入历史更 ...

  2. 串口通信n

    1.USART_Init(参数1,参数2) 串口配置步骤 1.串口时钟使能,GPIO使能 2.串口复位 3.端口模式设置GPIO_Init() 4.串口参数初始化USART_Init() 5,使能串口 ...

  3. 解决kvm虚拟机启动之后,网卡eth0变为eth1问题

    2018-12-19 故障前提 kvm虚拟机迁移到其他服务器上之后,重新启动网卡会出现问题 例如原网卡名称为eth0,迁移重启之后会自动变为eth1 为什么eth0会变成eth1? 很多Linux d ...

  4. java实现图片文字识别的两种方法

    一.使用tesseract-ocr 1.    https://github.com/tesseract-ocr/tesseract/wiki上下载安装包安装和简体中文训练文件 window64位安装 ...

  5. storm(5)-分布式单词计数例子

    例子需求: spout:向后端发送{"sentence":"my dog has fleas"}.一般要连数据源,此处简化写死了. 语句分割bolt(Split ...

  6. iozone文件系统测试工具在AM335x上的移植

     IOzone下载    下载地址:http://www.iozone.org 如下: 解压iozone,并进入到解压路径下的src/current 我的是  iozone3_487 命令:cd  i ...

  7. Netstat 的 10 个基本用法

    Netstat 简介 Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字. ...

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

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

  9. Net操作Excel,不依赖服务器端环境配置(终极方法NPOI)转。

    这是起因,为什么会需要用到这个,主要是分析了一下为什么从oledb那个方式换成这个方式.文章见链接 http://www.cnblogs.com/Jerseyblog/p/6410703.html 前 ...

  10. Go语言小试牛刀---几个简单的例子

    整理资料,发现之前手写的Go语言资料,现在贴过来. 第一个:Channel的使用,创建一个随机数 package main import "fmt" import "ru ...