Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13518    Accepted Submission(s): 5128

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<stdio.h>

#include<string.h>

#define INF 1 << 30

int map[101][101] ;

int dis[101] ;

int used[101] ;

void prim( int N )



 for(int k = 1 ; k <= N ; k++)

 {

  dis[k] = map[1][k] ;

  used[k] = 0 ;

 }

 int sum = 0 ;

 for(int i = 1 ; i <= N ; i++ )

 {

  int min = INF ;

  int c = 0 ;

  for(int j = 1 ; j <= N ; j++ )

  {

   if(!used[j] && dis[j] < min )

   {

    min = dis[j] ;

    c = j ;

   }

  }

  used[c] = 1 ;

  for(j = 1 ; j <= N ; j++)

  {

   if(!used[j] && dis[j] > map[c][j])

    dis[j] = map[c][j] ;

  }

 }

for(i = 1 ; i <= N ; i++)

  sum += dis[i] ;

 printf("%d\n", sum);

}

int main()

{

 int N = 0 ;

 while(~scanf("%d" , &N))

 {

     memset(map , 0 , sizeof(map) ) ;

  for(int i = 1 ; i <= N ; i++)

  {

   for(int j = 1 ; j <= N ; j++)

   {

    scanf("%d" , &map[i][j]) ;

   }

  }

  int Q = 0 ;

  scanf("%d" , &Q) ;

  int x = 0 , y = 0 ;

        for( int m = 1 ; m <= Q ; m++ )

  {

   scanf("%d%d" , &x , &y ) ;

   map[x][y] = map[y][x] = 0 ;//已经建好的树不用再建了

  }

  prim( N ) ;

 }

 return 0 ;

}

杭电1102 Constructing Roads的更多相关文章

  1. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  2. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  3. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  4. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  5. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  6. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  7. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1102 Constructing Roads(kruskal)

    Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...

  9. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

随机推荐

  1. BZOJ 1230 Usaco2008 Nov 开关灯 线段树

    思路: 用线段树模拟题中的操作就好 (标记异或 长度=区间总长度-当前已开灯的长度) //By SiriusRen #include <cstdio> using namespace st ...

  2. thinkserer TD350 系统损坏后,数据恢复及系统重做过程

    电脑配置: 联想服务器 TD350   E5-2609V4 2*8G 2*4T+R1 塔式 单电 1.系统恢复: 试过很多种方法,均无效 2.数据恢复:  重新安装系统后,直接在D盘查找 , 原C盘的 ...

  3. ZOJ 3175 Number of Containers 分块

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3216 乱搞的...watashi是分块做的...但我并不知道什么是分块...大 ...

  4. 【Henu ACM Round#15 A】 A and B and Chess

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计大写和小写的个数. 比较答案.输出即可. [代码] #include <bits/stdc++.h> using n ...

  5. Template template parameter(模板參数) example

    /********************************************************************************* Copyright (C), 19 ...

  6. jquery08

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 如何让hudson的两个job共用一个svn工作目录

    作者:朱金灿 来源:http://blog.csdn.net/clever101 现在我的需求是这样的:一个软件需要编译完全版本和基础版本,完全版本的基础功能较多,基础版本只包含了基础功能.有时只需要 ...

  8. 用SSL保Samba安全

    用SSL保Samba安全          在企业中用Samba做为文件服务器是非常容易的事了,那如何保证存储数据的安全,如何保证数据传输的安全呢?我以前介绍过通过Samba安全级别和加装防病毒软件在 ...

  9. SKU=Stock Keeping Unit(库存量单位)。即库存进出计量的单位,可以是以件,盒,托盘等为单位

    SKU=Stock Keeping Unit(库存量单位).即库存进出计量的单位,可以是以件,盒,托盘等为单位.SKU这是对于大型连锁超市DC(配送中心)物流管理的一个必要的方法.现在已经被引申为产品 ...

  10. pycharm做什么

    pycharm做什么 说实话.作为一个Coder.每天在各种IDE中切换编写Code.如果一个IDE Look and Feel总是无形中影响你每天Code Farm的心情.那该是多么不爽的事情.特别 ...