Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11894    Accepted Submission(s): 4496

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
 
核心思路:prim 算法,将已经修好的路置为0,例:1-2 已经修好路,邻接矩阵上表示时置为g[1][2]=g[2][1]=0;
 
AC代码:
 #include<stdio.h>
#include<string.h>
int g[][];
int ans=;;
void prim(int n)
{
int lowcost[],closet[],min=0xfffff;
int i,j,k;
int used[];
memset(used,,sizeof(used));
for(i=;i<=n;i++)
lowcost[i]=g[i][],
//printf("%d\n",lowcost[i]),
closet[i]=;
used[]=;
for(i=;i<n;i++)
{
min=0xfffff;
j=;
for(k=;k<=n;k++)
{
if(lowcost[k]<min&&(!used[k]))
min=lowcost[k],
j=k;
}
used[j]=;
ans+=g[j][closet[j]];
//printf("(%d %d)",j,closet[j]);
for(k=;k<=n;k++)
{
if(g[k][j]<=lowcost[k]&&(!used[k]))
{
lowcost[k]=g[k][j];
closet[k]=j;
}
}
//printf("\n");
//for(k=1;k<=n;k++)
//printf("closet[%d]=%d,low=%d used=%d\n",k,closet[k],lowcost[k],used[k]);
}
}
int main()
{
int n,i,j,m,a,b,x;
while(scanf("%d",&n)!=EOF)
{
ans=;
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&g[i][j]);
scanf("%d",&m);
for(i=;i<=m;i++)
scanf("%d %d",&a,&b),
g[a][b]=g[b][a]=;
prim(n);
printf("%d\n",ans);
}
return ;
}

HDOJ 1102 生成树的更多相关文章

  1. hdoj 1102 Constructing Roads

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 分析:看到这题给出的都是矩阵形式,就知道了可以用Prim算法求MST. #include <i ...

  2. (MST) HDOJ 1102 Constructing Roads

    怎么说呢 这题就是个模板题 但是 hud你妹夫啊说好的只有一组数据呢??? 嗯??? wa到家都不认识了好吗 #include <cstdio> #include <cstring& ...

  3. Hdoj 1102.Constructing Roads 题解

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  4. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  5. 【HDOJ】4305 Lightning

    1. 题目描述当一个结点lightning后,可以向其周围距离小于等于R的结点传播lightning.然后以该结点为中心继续传播.以此类推,问最终形成的树形结构有多少个. 2. 基本思路生成树级数模板 ...

  6. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  7. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  8. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. Linq之隐式类型、自动属性、初始化器、匿名类

    目录 写在前面 系列文章 隐式类型 自动属性 初始化器 匿名类 总结 写在前面 上篇文章是本系列的小插曲,也是在项目中遇到,觉得有必要总结一下,就顺手写在了博客中,也希望能帮到一些朋友.本文将继续介绍 ...

  2. jQuery基础之(六)jQuery浏览器的兼容性(以CSS3特写兼容举例)

    CSS3充分吸收多年了web发展的需求,吸收了很多新颖的特性.例如border-radius css3特性中最令人兴奋的莫过于选择器的增强,例如属性选择器可以根据某个属性值来选择标记,位置选择器可以根 ...

  3. 让less编译通过css滤镜

    写IE6 hack的时候,发现在less中直接写css滤镜是会报错的,不能编译通过. 解决方法为:用~“”把相关的css代码包裹起来,例如: _top:~"expression(docume ...

  4. codevs 1378选课 树形DP

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ],tr[] ...

  5. Nginx Installation、Configuration、Rreverse Proxy、Load Balancing Learning

    目录 . Nginx简介 . Nginx安装部署 . Nginx安全配置 . Nginx反向代理实践 . Nginx负载均衡实践 1. Nginx简介 0x1: Nginx的基本特性 Nginx(&q ...

  6. java + jquery + ajax + json 交互

    前端js部分: $.ajax({ async:true, cache:false, type:"POST", dataType : 'json', url:"/shopp ...

  7. UVA 1626 Brackets sequence(括号匹配 + 区间DP)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...

  8. UVA12563Jin Ge Jin Qu hao(01背包)

    紫书P274 题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长:最后必唱<劲歌金曲> 所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲 ...

  9. Eclipse开发Android程序如何在手机上运行

    android开发不论是在真机上调试还是最终发布到真机上都非常简单,过程如下: 1.安装usb驱动 手机要能与电脑相连,当然要安驱动了.效果就是你插入手机,电脑显示驱动已识别.驱动安装的官方教程:ht ...

  10. mysql 存储过程中注意的问题

    OPEN cur; REPEAT FETCH cur INTO int_type, int_element_id, int_num, int_user_id; IF NOT _DONE THEN IF ...