zoukankan      html  css  js  c++  java
  • HDU1102--Constructing Roads

    Constructing Roads
    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10598 Accepted Submission(s): 3942


    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


    Source
    kicc


    Recommend
    Eddy

    简单最小生成树

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #include<iostream>
      5 #include<cmath>
      6 #include<map>
      7 using namespace std;
      8 
      9 int parent[5001];
     10 int n;
     11 int edge[1001][1001];
     12 
     13 struct node
     14 {
     15     int u,v;
     16     int w;
     17 }point[5001];
     18 
     19 bool cmp(node a,node b)
     20 {
     21     return a.w<b.w;
     22 }
     23 
     24 void Ufset()
     25 {
     26     int i;
     27     for(i=1;i<=5000;i++)
     28     {
     29         parent[i]=-1;
     30     }
     31 }
     32 
     33 int Find(int x)
     34 {
     35     int s;
     36     for(s=x;parent[s]>0;s=parent[s]){}
     37     while(s!=x)
     38     {
     39         int temp=parent[x];
     40         parent[x]=s;
     41         x=temp;
     42     }
     43     return s;
     44 }
     45 
     46 void Union(int R1,int R2)
     47 {
     48     int r1=Find(R1),r2=Find(R2);
     49     int temp=parent[r1]+parent[r2];
     50     if(parent[r1]>parent[r2])
     51     {
     52         parent[r1]=r2;
     53         parent[r2]=temp;
     54     }
     55     else
     56     {
     57         parent[r1]=temp;
     58         parent[r2]=r1;
     59     }
     60 }
     61 
     62 int kruskal(int x)
     63 {
     64     int sum=0;
     65     int i;
     66     for(i=1;i<x;i++)
     67     {
     68         int u=point[i].u,v=point[i].v;
     69         if(Find(u)!=Find(v))
     70         {
     71             Union(u,v);
     72             sum+=point[i].w;
     73         }
     74     }
     75     return sum;
     76 }
     77 
     78 int main()
     79 {
     80     while(scanf("%d",&n)!=EOF)
     81     {
     82         memset(edge,0,sizeof(edge));
     83         Ufset();
     84         int i,j;
     85         int cnt=1;
     86         for(i=1;i<=n;i++)
     87         {
     88             for(j=1;j<=n;j++)
     89             {
     90                 scanf("%d",&edge[i][j]);
     91                 if(j>i)
     92                 {
     93                     point[cnt].u=i;
     94                     point[cnt].v=j;
     95                     point[cnt++].w=edge[i][j];
     96                 }
     97             }
     98         }
     99         sort(point+1,point+cnt,cmp);
    100         int q;
    101         scanf("%d",&q);
    102         for(i=1;i<=q;i++)
    103         {
    104             int u,v;
    105             scanf("%d%d",&u,&v);
    106             if(Find(u)!=Find(v))
    107             {
    108                 Union(u,v);
    109             }
    110         }
    111         int ans=kruskal(cnt);
    112         printf("%d
    ",ans);
    113     }
    114     return 0;
    115 }
    View Code
  • 相关阅读:
    DirectX编译环境配置
    [转]unresolved external symbol __imp__PlaySoundA@12的解决方法
    Win7/Win8/Win10显示桌面按钮
    Win8中更改账户信息
    3D游戏图形引擎
    【整理】鼠标位置编码(Mouse Position Code)和鼠标激活返回值(MOUSEACTIVATE Return Codes)
    精简ICO图标可减小EXE程序文件大小
    【整理】SYSCOMMAND的wParam值的宏定义
    迅雷网速测试器 下载速率测试记录
    【整理】窗体消息(Window Messages) 的宏定义
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3199712.html
Copyright © 2011-2022 走看看