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. legend---六、php脚本变量的生命周期是怎样的

    legend---六.php脚本变量的生命周期是怎样的 一.总结 一句话总结:应该是脚本结束变量的生命周期就完了 1.外部js找不到元素是怎么回事? 1 function myDailyTaskFin ...

  2. hibernate generator id

    以下内容整理自网络 “assigned” 主键由外部程序负责生成,在   save()   之前指定一个.  “hilo” 通过hi/lo   算法实现的主键生成机制,需要额外的数据库表或字段提供高位 ...

  3. C/C++(C++类与对象)

    构造器(constructor) 1.与类名相同,无返回,被系统生成对象时自动调用,用于初始化. 2.可以有参数,构造器的重载,有默认参数.重载和默认参数不能同时出现,但是一定要包含标配(无参数的构造 ...

  4. python 多线程学习小记

    python对于thread的管理中有两个函数:join和setDaemon setDaemon:如果在程序中将子线程设置为守护线程,则该子线程会在主线程结束时自动退出,设置方式为thread.set ...

  5. 【Uva 1629】 Cake slicing

    [Link]: [Description] 给你一个n*m的格子; 然后里面零零散散地放着葡萄 让你把它切成若干个小矩形方格 使得每个小矩形方格都恰好包含有一个葡萄. 要求切的长度最短; 问最短的切割 ...

  6. Linux学习总结(5)——CentOS常用的目录文件操作命令

    CentOS常用的目录文件操作命令 一.路径操作的CentOS常用命令  cd pwd  NO1. 显示当前路径  [root@rehat root]# pwd  NO2. 返回用户主目录  [roo ...

  7. C++的new_handler

    这个new_handler其实对应于signal_handler 当operator new申请一个内存失败时,它会进行如下的处理步骤:1.如果存在客户指定的处理函数,则调用处理函数(new_hand ...

  8. sleep实现原理

    用户程序中的睡眠: sleep()    usleep()    nanosleep() sleep()和nanosleep()都是使进程睡眠一段时间后被唤醒,但是二者的实现完全不同.Linux中并没 ...

  9. python编程练习

    python练习之冒泡排序: python代码: #coding=utf-8 if __name__=="__main__": arr=[3,2,1,7,11,4,5,8] pri ...

  10. 零基础学python-7.6 字符串格式化表达式

    字符串格式化同意在一个单个的步骤中对一个字符串运行多个特定类型的替换 特别是给用户提示的时候,格式化很方便 实现方法: 1.格式化表达式,类似于c语音的printf 在表达式中,我们使用%二进制操作符 ...