题目链接: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. php定时自动执行 需启动第一次

    1 2 3 4 5 6 7 8 9 10 11 12 <? ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行. set_time_li ...

  2. django-celery

    在settings.py里installed_apps里添加 djcelery 需安装django-celery 在settings末端添加 import djcelery djcelery.setu ...

  3. 经验总结:按需加载JS和css

    项目中做过这样的事情:所有页面都通过SSI指令 include这样一份public-js.shtml, 用来引入涉及到的js(包括公共的脚本 验证插件 自定义组件等),但是一些没有交互效果的页面根本不 ...

  4. hibernate Annotation 以及注解版的数据关联 4.4

    目的是不写xxx.hbm.xml映射文件,使用注解 主配置文件还是要有hibernate.cfg.xml <?xml version="1.0" encoding=" ...

  5. centos7 在 vmware下的安装与配置

    我们这里选择的centos7的版本是  CentOS-7-x86_64-DVD-1511.iso 在vmware下安装,碰到两个问题: 1)安装如下界面时,会发现“开始安装”按钮是灰化的,这时需要对有 ...

  6. Objective-C中的类目(Category),延展(Extension)

    类目和延展的作用都是为了扩展一个类. Objective-C中的类目(Category) 一.类目的定义和作用 类目也叫分类,英文Category,在没有原类.m文件的基础上,给该类添加方法. 比如, ...

  7. 和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧。因为,很多PCI的例子都是对S5933,就连微软出版的《Programming the Microsoft Windows Driver Model》都提供了一个完整的S5933的例子。 在这篇有关DDK的开发论文里。

    和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧.因为,很多PCI的例子都是对S5933,就连微软出版的<Programming the Microsoft Wi ...

  8. 免费edu邮箱申请注冊地址

    几个国外.edu邮箱注冊地址: 注冊地址:http://mail.alumni.fandm.edu/reg/reg_pangia.asp   @alumni.fandm.edu 注冊地址: http: ...

  9. linux中/etc与/var目录,各是什么意思?这两个目录下的文件有什么特点?

    http://zhidao.baidu.com/link?url=DkxU9CyhJb_dIUAPCmPmxRtQsENgCzqy5qnLPEj_V9DqNzdt6Qya0U5iCVRCYFkgoRo ...

  10. String类的一些方法

    String 类有以下方法: startsWith(String prefix) boolean java.lang.String.startsWith(String prefix) Tests if ...