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
    解题思路:

要修公路,输入一个n,表示n个村庄。接着输入n*n的矩阵,然后输入一个q 接下来的q行

每行包含两个数a,b,表示a、b这条边联通,就是已经有公路不用修了,要让所有村庄联通在一起问:修路最小代价?

最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法。

代码如下:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. int N;
  7. int dis[][];
  8. bool vis[][];
  9. struct edge{
  10. int u;
  11. int v;
  12. int w;
  13. }e[];
  14. int pre[];
  15. int find(int x)
  16. {
  17. return (x==pre[x])?x:pre[x] = find(pre[x]);
  18. }
  19. bool cmp(edge a , edge b)
  20. {
  21. return a.w<b.w;
  22. }
  23. int Q;
  24. int main()
  25. {
  26. scanf("%d",&N);
  27. for(int i = ; i <= N;i++)
  28. {
  29. pre[i] = i;
  30. }
  31. for(int i = ; i <= N ;i++)
  32. {
  33. for(int j = ; j <= N ;j++)
  34. {
  35. scanf("%d",&dis[i][j]);
  36. }
  37. }
  38.  
  39. int x , y;
  40. scanf("%d",&Q);
  41. int edge_num = ;
  42. while(Q--)
  43. {
  44. scanf("%d%d",&x,&y);
  45. dis[x][y] = ;
  46. dis[y][x] = ;
  47. }
  48. for(int i = ;i <= N ;i++)
  49. {
  50. for(int j = ; j <= N; j++)
  51. {
  52.  
  53. e[edge_num].u = i;
  54. e[edge_num].v = j;
  55. e[edge_num].w = dis[i][j];
  56. edge_num++;
  57. }
  58. }
  59.  
  60. sort(e,e+edge_num,cmp);
  61. int u , v , w;
  62. int fx ,fy;
  63. int ans = ;
  64. for(int i = ; i < edge_num ;i++)
  65. {
  66. u = e[i].u;
  67. v = e[i].v;
  68. w = e[i].w;
  69.  
  70. fx = find(u);
  71. fy = find(v);
  72.  
  73. if(fx!=fy)
  74. {
  75. pre[fx] = fy;
  76. ans += w;
  77. }else
  78. {
  79. continue;
  80. }
  81. }
  82. printf("%d\n",ans);
  83. return ;
  84. }

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

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

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

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

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

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

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

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

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

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

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

  6. [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads

    给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...

  7. Poj 2421 Constructing Roads(Prim 最小生成树)

    题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...

  8. POJ - 2421 Constructing Roads(最小生成树&并查集

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  9. poj 2421 Constructing Roads 解题报告

    题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...

随机推荐

  1. chrome浏览器控制台创建js脚本并执行

    Chrome的snippets是小脚本,还可以创作并在Chrome DevTools的来源面板中执行.您可以访问和从任何页面运行它们.当你运行一个片段,它从当前打开的页面的上下文中执行.本文主要讲如何 ...

  2. 利用UUID 随机生成8位短号

    //获得8位短号 public static String[] chars = new String[] { "a", "b", "c", ...

  3. [转]NDK编译库运行时报dlopen failed: cannot locate symbol "__exidx_end" 解决办法

    原文链接:http://blog.csdn.net/acm2008/article/details/41040015 当用NDK编译的库在运行加载时报如下错: dlopen("/data/d ...

  4. ubuntu 14.04 Clion2016.2 安装激活与安装后添加快捷启动方式

    参考链接:http://www.cnblogs.com/conw/p/5938113.html 下载clion for linux : http://www.jetbrains.com/clion/d ...

  5. 爬虫框架scrapy的基本内容

    Scrapy介绍 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以帮助用户简单快速的部署一个专业的网络爬虫.如果说前面我们写的定制bs4爬虫是”手动挡“,那Scrapy就相当 ...

  6. javascript总结39: 元素获取的常见问题

    1 定义id属性的元素,不获取直接使用 由于id名具有唯一性,部分浏览器支持直接使用id名访问元素,但不是标准方式,生产环境下不推荐使用. 2 元素是对象 获取到的元素是DOM对象 ,DOM对象也有数 ...

  7. CodeForces 474C Captain Marmot (数学,旋转,暴力)

    题意:给定 4n * 2 个坐标,分成 n组,让你判断,点绕点的最少次数使得四个点是一个正方形的顶点. 析:那么就一个一个的判断,n 很小,不会超时,四个点分别从不转然后转一次,转两次...转四次,就 ...

  8. 团体程序设计天梯赛L1-024 后天 2017-03-22 17:59 68人阅读 评论(0) 收藏

    L1-024. 后天 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就 ...

  9. c语言和java以及安卓和苹果

    苹果手机是本地,没有中间环节,速度快,基于Linux系统 安卓是通过虚拟机,影响速度 就像c语言和java c适用于架构小的地方,因为直接编译运行 而java用于架构比较大的地方,启动慢,启动之后效率 ...

  10. SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管 ...