题目链接: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
 
Sample Output
179

代码例如以下:

#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define MAXN 517
//创建m二维数组储存图表,low数组记录每2个点间最小权值,visited数组标记某点是否已訪问
int m[MAXN][MAXN], low[MAXN], visited[MAXN];
int n;
int prim( )
{
int i, j;
int pos, minn, result=0;
// memset(visited,0,sizeof(visited));
for(i = 1; i <= n; i++)
visited[i] = 0;
visited[1] = 1;
pos = 1; //从某点開始,分别标记和记录该点
for(i = 1; i <= n; i++) //第一次给low数组赋值
{
if(i != pos)
low[i] = m[pos][i];
else
low[i] = 0;
}
for(i = 1; i <= n; i++) //再执行n-1次
{
minn = INF; //找出最小权值并记录位置
pos = -1;
for(j = 1; j <= n; j++)
{
if(visited[j]==0 && minn>low[j])
{
minn = low[j];
pos = j;
}
}
if(pos == -1)
continue;
result += minn; //最小权值累加
visited[pos] = 1; //标记该点
for(j = 1; j <= n; j++) //更新权值
if(!visited[j] && low[j]>m[pos][j])
low[j] = m[pos][j];
}
return result;
}
int main()
{
int tt;
while(~scanf("%d",&n))
{
memset(m,INF,sizeof(m)); //全部权值初始化为最大
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d",&tt);
if(tt < m[j][i])
m[i][j] = m[j][i] = tt;
}
}
int Q, a, b;
scanf("%d",&Q);
for(int i = 0; i < Q; i++)
{
scanf("%d%d",&a,&b);
m[a][b] = m[b][a] = 0;
}
int ans = prim( );
printf("%d\n",ans);
}
return 0;
}

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 (最小生成树)

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

  3. hdu 1102 Constructing Roads (Prim算法)

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

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

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

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

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

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

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

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

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

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

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

  9. hdu 1102 Constructing Roads Kruscal

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

随机推荐

  1. 「OC」构造方法和分类

    一.构造方法 (一)构造方法的调用 创建一个可用的对象:Person *p=[Person new]; new方法实际上是分为两步来创建一个对象: 1)使用+alloc方法来分配存储空间(返回分配的对 ...

  2. 我的Python成长之路---第六天---Python基础(20)---2016年2月20日(晴)

    一.面向对象基础 面向对象名词解释: 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公 ...

  3. urllib2使用2

    Timeout 设置 import urllib2 response = urllib2.urlopen('http://www.google.com', timeout=10) 在 HTTP Req ...

  4. 实用推荐:12款Linux系统恢复工具

    12款Linux系统恢复工具 电脑死机,系统崩溃,总会给电脑使用者带来一定的损失.你是否不小心删除你的纪念图片?安装新系统时候,擦除了分区表?无法读取旧CD里面的数据?别急嘛-我们将会给您推荐一些免费 ...

  5. BZOJ 2440 完全平方数

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 966  Solved: 457 [Submit][Sta ...

  6. Shiro入门(1)

    =============基本概念=================== 什么是Apache Shiro? Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是 ...

  7. haml、sass简单的解释

    1. Haml 全名为 HTML Abstract Markup Language,主要就是让开发者能够使用缩排的方式撰写 HTML,做到永不忘记关 Tag 的效果. 例如:%h1= "He ...

  8. B - 畅通工程(并查集)

    对并查集理解之后就可以做这种题了,虽说这种题做的不多,这道题做过才这么快搞定,可是还是挺happy滴,加油 Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接 ...

  9. ulipad双击无反应

    所有的东西都配好后,执行ulipad需要注意的是: 1,必须以管理员身份运行ulipad. 2,当运行有道词典的时候,双击ulipad是没有反应, 至于为什么会出现这种情况,我也不太清除,等我查到 原 ...

  10. Content Providers的步骤,来自官网文档

    Content Providers In this document Content provider basics Querying a content provider Modifying dat ...