zoukankan      html  css  js  c++  java
  • HDU 1102 Constructing Roads

    Constructing Roads




     

    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
     
     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 int gp[105][105];
     6 int lowercost[105];
     7 int used[105];
     8 int n;
     9 
    10 int prim()
    11 {
    12     int i,j,k;
    13     int sum=0;
    14     used[1]=1;
    15     for(i=1;i<=n;i++)
    16     lowercost[i]=gp[1][i];
    17     for(i=2;i<=n;i++)
    18     {
    19         int pos;
    20         int temp=999999;
    21         for(j=1;j<=n;j++)
    22         {
    23             if(lowercost[j]<temp&&!used[j])
    24             {
    25                 temp=lowercost[j];
    26                 pos=j;
    27             }
    28         }
    29         used[pos]=1;
    30         sum+=lowercost[pos];
    31         for(k=1;k<=n;k++)
    32         {
    33             if(gp[pos][k]<lowercost[k]&&!used[k])
    34             lowercost[k]=gp[pos][k];
    35         }
    36     }
    37     return sum;
    38 }
    39 
    40 int main()
    41 {
    42     int i,j,m,a,b;
    43     while(scanf("%d",&n)!=EOF)
    44     {
    45         memset(lowercost,0,sizeof(lowercost));
    46         memset(used,0,sizeof(used));
    47         for(i=1;i<=n;i++)
    48         for(j=1;j<=n;j++)
    49         scanf("%d",&gp[i][j]);
    50         scanf("%d",&m);
    51         while(m--)
    52         {
    53             scanf("%d%d",&a,&b);
    54             gp[a][b]=gp[b][a]=0;
    55         }
    56         int ans=prim();
    57         printf("%d
    ",ans);
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    Android 关于屏幕适配
    android 数据存储之SharePreference 的几种方式
    Android多项目依赖在Eclipse中无法关联源代码的问题解决 Ctril 点不进去的解决方法
    android onIntent 是什么东西
    Android一次退出所有Activity的方法(升级版)
    Android 一次退出所有activity的方法
    android 获取屏幕尺寸
    自定义view(自定义view的时候,三个构造函数各自的作用)
    Android应用自动更新功能的实现!!!
    android实现透明和半透明效果
  • 原文地址:https://www.cnblogs.com/homura/p/4717339.html
Copyright © 2011-2022 走看看