链接:

http://poj.org/problem?id=2421

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21130   Accepted: 8924

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

  1. 3
  2. 0 990 692
  3. 990 0 179
  4. 692 179 0
  5. 1
  6. 1 2

Sample Output

  1. 179

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int N = ;
  8. const int INF = 0xfffffff;
  9.  
  10. int n, J[N][N], dist[N], vis[N];
  11.  
  12. int Prim()
  13. {
  14. int i, j, ans=;
  15. dist[]=;
  16. memset(vis, , sizeof(vis));
  17. vis[]=;
  18.  
  19. for(i=; i<=n; i++)
  20. dist[i]=J[][i];
  21.  
  22. for(i=; i<n; i++)
  23. {
  24. int index=, MIN=INF;
  25. for(j=; j<=n; j++)
  26. {
  27. if(!vis[j] && dist[j]<MIN)
  28. {
  29. index=j; MIN=dist[j];
  30. }
  31. }
  32. vis[index]=;
  33. ans += MIN;
  34. for(j=; j<=n; j++)
  35. {
  36. if(!vis[j] && dist[j]>J[index][j])
  37. dist[j]=J[index][j];
  38. }
  39. }
  40. return ans;
  41. }
  42.  
  43. int main ()
  44. {
  45. while(scanf("%d", &n)!=EOF)
  46. {
  47. int i, j;
  48.  
  49. memset(J, , sizeof(J));
  50.  
  51. for(i=; i<=n; i++)
  52. for(j=; j<=n; j++)
  53. scanf("%d", &J[i][j]);
  54.  
  55. int m, a, b;
  56. scanf("%d", &m);
  57. for(i=; i<=m; i++)
  58. {
  59. scanf("%d%d", &a, &b);
  60. J[a][b]=J[b][a]=;
  61. }
  62.  
  63. int ans=Prim();
  64.  
  65. printf("%d\n", ans);
  66. }
  67. return ;
  68. }

(最小生成树)Constructing Roads -- poj -- 2421的更多相关文章

  1. Constructing Roads POJ - 2421 (最小生成树)

    思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将    所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...

  2. Constructing Roads POJ - 2421 最小生成树板子题

    #include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...

  3. Constructing Roads POJ - 2421

    题目链接:https://vjudge.net/problem/POJ-2421 思路:一些村庄,建一些路,使得所有村庄能相连,而且使得所有路长度之和最短. 题目说了,有些村庄之间已经建了路,说明有些 ...

  4. (最小生成树) Jungle Roads -- POJ -- 1251

    链接: http://poj.org/problem?id=1251 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2177 ...

  5. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  6. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  7. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

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

  8. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

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

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

随机推荐

  1. Jquery和Ajax

    jQuery 是一个 JavaScript 函数库.JavaScript 是 HTML5 以及所有现代浏览器中的默认脚本语言! jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 ...

  2. Ansiable Manage MySQL global variables

    mysql_variables - Manage MySQL global variables New in version 1.3. Synopsis Requirements (on host t ...

  3. Linux下php5.3.3安装mcrypt扩展

    具体操作: 一.下载软件包 1.下载php(版本要与系统安装的一致) http://pan.baidu.com/s/1mifTbfE 2.下载libmcrypt(安装mcrypt需要此软件包) htt ...

  4. cmd 获取 拖拽文件名

    1. @echo off & setlocal enableDelayedExpansion set a= set /p a=Please drag your txt file for spl ...

  5. 第七章 二叉搜索树(d4)AVL树:(3+4)-重构

  6. git pull和git fetch命令

    git pull和git fetch命令 git pull git pull命令的作用是取回远程主机某个分支的更新,在与本地指定分支合并,格式如下: $ git pull <远程主机名>& ...

  7. Halcon的C#二次开发及经验分享

    本文涉及面较广,因此很难在所有方面都讲解得很详细,故适合具有一定Halcon开发经验的人阅读. 1.Halcon二次开发的两种方式 ① 使用C#的语法方式逐句改写Halcon代码 优点:各种变量的类型 ...

  8. Python3编程技巧

    高效处理数据类型方法: In []: from random import randint In []: data=[randint(-,) )] In []: data Out[]: [-, -, ...

  9. linux信号处理总结

    本文主要讲解常见信号的处理方式. Sighup:终端关闭时,发送给此会话的所有进程组.Setsid成功后不再属于该会话,收不到该消息. Sigterm:  kill process_id时产生. Si ...

  10. laravel中的old()函数

    1.控制器 2.模板