题目链接: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. 「Foundation」字符串

    一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1)NSArray:OC不可变数组  NSMutableA ...

  2. 「OC」类和对象

    一.面向对象 OC语言是面向对象的,c语言是面向过程的,面向对象和面向过程只是解决问题的两种思考方式,面向过程关注的是解决问题涉及的步骤,面向对象关注的是设计能够实现解决问题所需功能的类. 术语:OO ...

  3. 复制virtualenv环境到其他服务器环境配置的方法

    要在n多服务器端部署python的应用,虽然python本身是跨平台的,当时好多第三方的扩展却不一定都能做到各个版本兼容,即便是都是linux,在redhat系列和ubuntu系列之间来回导也是个很让 ...

  4. (zz)Linux下Gcc生成和使用静态库和动态库详解

    http://blog.chinaunix.net/uid-23592843-id-223539.html

  5. [LeetCode]题解(python):062-Unique Paths

    题目来源: https://leetcode.com/problems/unique-paths/ 题意分析: 给定两个整型m,n.判断从一个m×n的矩阵里面,从(0,0)走到(m-1,n-1)一共有 ...

  6. pip install 出现报asciii码错误的解决

    原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码.解决办法是: python目录 Python27\Lib\site-packages 建一个文件site ...

  7. c++ 复制构造函数和赋值函数

    c++ 自动提供了下面这些成员函数 1默认构造函数 2.复制构造函数 3.赋值操作符 4.默认析构函数 5.地址操作符 赋值构造函数copy construtor 用于将一个对象复制到新创建的对象中, ...

  8. 破解企业QQ对个人QQ登陆的限制(原创)

    运行系统:WIN7 破解时间:2014-02-19 破解思路:自从2013-11-11的1.85版企业qq更新后,网上流传的破解方法(运行文件BeatQQEIM.bat)已经不起作用了,可以同时登陆, ...

  9. pod update --verbose --no-repo-update

    最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...

  10. Linux编程---I/O部分

    非常多函数都能够在网上找到,也比較基础,所以原型仅仅给出了函数名.详细用到再man吧. 输入输出是个非常重要的一块内容.差点儿网络相关的东西基本都是靠底层IO调用来实现的. 好吧.还是先踏踏实实的介绍 ...