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. 用wcf实现带有“秒传”功能的网盘

    写在前面 前面记录过这样一个关于“秒传”的实现思路,在这篇就弄了一个简单的demo实现了一下,当中有很多业务仍没考虑,只是将“秒传”的实现思路,用代码实现了一下. 关于秒传,可以参考这篇文章:何为“秒 ...

  2. Grunt-cli的执行过程以及Grunt加载原理

    通过本篇你可以了解到: 1 grunt-cli的执行原理 2 nodeJS中模块的加载过程 Grunt-cli原理 grunt-cli其实也是Node模块,它可以帮助我们在控制台中直接运行grunt命 ...

  3. 压缩算法实现之LZ78

    LZ78编码 LZ78算法,建立词典的算法. LZ78的编码思想: 不断地从字符流中提取新的缀-符串(String),通俗地理解为新"词条",然后用"代号"也就 ...

  4. WebForm控件之DropDownList

    DropDwonList 三件事: ------------------------------------------1.把内容填进去-------------------------------- ...

  5. 使用Owin中间件搭建OAuth2.0认证授权服务器

    前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...

  6. 负margin一些奇葩的布局技巧

    copy_from_ http://www.hicss.net/i-know-you-do-not-know-the-negative-margin/ <!doctype html> &l ...

  7. MongoDB的安装及配置

    MongoDB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐. Windows (1). 登录Mongodb官网点击下载 (2). 将zi ...

  8. BIEE 后台新建分析没有你创建的数据源

    (1)登录http://win-5rnnibkasrt:9704/analytics/saw.dll?bieehome  点击“管理” 找到“发出SQL语句”在里面写call sapurgeallca ...

  9. JAVA-基本数据类型与引用数据类型区别

    package com.liu.u6.copy1; /* * 基本数据类型与引用数据类型有什么区别 */ public class Sjlx { public int age; } package c ...

  10. TOJ3540Consumer(有依赖的背包)

    http://acm.tju.edu.cn/toj/showp3540.html3540.   Consumer Time Limit: 2.0 Seconds   Memory Limit: 655 ...