F. Constructing Roads

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
  2.  
  3. 题意:
    N个村庄,编号从1N。现需要在这N个村庄之间修路,使得任何两个村庄之间都可以连通。称AB两个村庄是连通的,
    当且仅当AB有路直接连接,或者存在村庄C,使得AC两村庄之间有路连接,且CB之间有路连接。已知某些村庄之间已经有
    路直接连接了,试修建一些路使得所有村庄都是连通的、且修路总长度最短。
  1. #include <cstdio>
  2. #include <iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7. const int MAXN=;
  8. int p[MAXN];
  9. bool sum[MAXN];
  10. int m[MAXN][MAXN];
  11. struct node
  12. {
  13. int x,y,l;
  14. }a[];
  15. bool cmp(node a,node b)
  16. {
  17. return a.l<b.l;
  18. }
  19. int Find(int x)
  20. {
  21. return x==p[x]?x:(p[x]=Find(p[x]));
  22. }
  23. int Union(int R1,int R2)
  24. {
  25.  
  26. int r1=Find(R1);
  27. int r2=Find(R2);
  28. if(r1!=r2)
  29. {
  30. p[r1]=r2;
  31. return ;
  32. }
  33. else return ;
  34. }
  35. int main()
  36. {
  37. int n;
  38. int cnt=,i,j;
  39. while(~scanf("%d",&n))
  40. {
  41. cnt =;
  42. memset(sum,,sizeof(sum));
  43. for(i=;i<=n;i++)
  44. p[i]=i;
  45. for(i=;i<=n;i++)
  46. for( j=;j<=n;j++)
  47. scanf("%d",&m[i][j]);
  48. int t,c,b;
  49. scanf("%d",&t);
  50. while(t--) //将已经修好的路长度清零
  51. {
  52. scanf("%d%d",&c,&b);
  53. m[c][b]=m[b][c]=;
  54. }
  55. int k=;
  56. for(i=;i<=n;i++)
  57. {
  58. for(j=+i;j<=n;j++)
  59. {
  60. a[k].x=i;
  61. a[k].y=j;
  62. a[k].l=m[i][j];
  63. k++;
  64. }
  65. }
  66. sort(a,a+k,cmp);
  67.  
  68. for(i=;i<k;i++)
  69. {
  70. if(Union(a[i].x,a[i].y)==)
  71. cnt+=a[i].l;
  72. }
  73. printf("%d\n",cnt);
  74. }
  75. return ;
  76. }

Constructing Roads——F的更多相关文章

  1. hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  2. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  3. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  4. HDU 1102 Constructing Roads

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

  5. Constructing Roads (MST)

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

  6. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  7. hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

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

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

  9. hdu 1102 Constructing Roads Kruscal

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

随机推荐

  1. web在线打印,打印阅览,打印维护,打印设计

    winform打印的方案比较多,实现也比较容易,而且效果也非常炫:但现在越来越多的系统是web系统,甚至是移动端.网上也有非常的web打印方案,但各式各样的问题非常多,比如js兼容性,稳定性等一直缠绕 ...

  2. Mongo DB 2.6 需要知道的一些自身限定

    在现实的世界中,任何事情都有两面性,在程序的世界中,亦然! 我们不论是在使用一门新的语言,还是一门新的技术,在了解它有多么的让人兴奋,让人轻松,多么的优秀之余,还是很有必要了解一些他的局限性,方便你在 ...

  3. 2013成都网络赛 J A Bit Fun(水题)

    A Bit Fun Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. http_build_query 的一个问题

    当我们使用CURL来post数据的时候,需要设置post的数据 curl_setopt($c, CURLOPT_POSTFIELDS, $post_data); 假如这里的$data是 $data = ...

  5. Linux 定时任务 Crontab命令 详解

    前言 crontab是Unix和Linux用于设置周期性被执行的指令,是互联网很常用的技术,很多任务都会设置在crontab循环执行,如果不使用crontab,那么任务就是常驻程序,这对你的程序要求比 ...

  6. PhpStorm 2016.3 For Mac 重大里程碑更新 -- 终于解决了不能输入中文标点符号的重大bug

    PhpStorm 2016.3 For Mac 重大里程碑更新 1.[终于解决了]不能输入中文标点符号的重大bug,如 逗号“,”.“.”: 2.可以在一个窗体中,同时打开多个项目: 3.其他... ...

  7. 用Canvas写桌球游戏!!!

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 昨天上班的时候闲着无事,就用Canvas写了个桌球游戏来玩玩....所以就拿这游戏上来水一发.或许对一些刚学canvas的人有帮助. 话说 ...

  8. sdcms标签

    模板防盗:<%if not in_sdcms then response.write("template load fail"):response.end() end if% ...

  9. Java 调用 C++ (Java 调用 dll)康哥手把手教你

    摘要: 本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4979243.html 因为要做点图形处理的项目,需要在Java中调用dll库,所以开发的第一步是 ...

  10. WindowsForm菜单工具栏--2016年12月6日

    ContextMenuStrip 添加控件后可在其他空间属性中进行绑定 MenuStrip       设置热键:在编辑的时候输入(&F)       设置快捷键:选中菜单项--右键属性--S ...