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. Javascript基础系列之(二)变量

    javascript 中变量通过var关键字(variable)来声明的. var school = "beijingyizhong" 也可以通过var 关键字给变量多个值. va ...

  2. PHP微信登錄(網頁授權)之後的獲取用戶的信息

    //這部峯代碼是封裝的庫文件,<?php /** * Created by PhpStorm. * User: root * Date: 16-6-23 * Time: 下午3:29 */ cl ...

  3. Codeforces Beta Round #6 (Div. 2 Only) D. Lizards and Basements 2 dp

    题目链接: http://codeforces.com/problemset/problem/6/D D. Lizards and Basements 2 time limit per test2 s ...

  4. iOS边练边学--级联菜单的两种实现方法

    一.方法1:如图,图中的两个tableView分别交给两个控制器来管理 重点难点:categoryTableView被点击之后,subcategoryTableView要取得相应的数据进行刷新,所以s ...

  5. Canvas识别相似图片

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. Spring 管理数据源

    Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...

  7. 【ZOJ 3609】Modular Inverse

    题 题意 求a关于m的乘法逆元 分析 a x ≡ 1 (mod m) 等价于 ax+my=1 求x的最小正数(不能是0,我就WA在这里了). 当m=1时,或者 gcd(a,m)!=1 时x不存在. 所 ...

  8. Oracle同义词创建及分配用户创建同义词权限

    (1)--授权某个用户crate synonym的权限,若用户名为scott grant create synonym to scott(2)--创建同义词 create [or replace] s ...

  9. JSP登录验证并显示信息

    加入C标签: 加入jstl.jar 和standard.jar加入Lib文件夹中 将c.tld放入WEB-Info文件夹中 index.jsp <%@ page language="j ...

  10. JSP 九个隐含JSP对象

    输入输出对象:request.response.out. 作用域通信对象:session.application.pageContext servlet对象:page.config 错误对象:exce ...