zoukankan      html  css  js  c++  java
  • HDOJ 1102 生成树

    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11894    Accepted Submission(s): 4496

    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
     
    核心思路:prim 算法,将已经修好的路置为0,例:1-2 已经修好路,邻接矩阵上表示时置为g[1][2]=g[2][1]=0;
     
    AC代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 int g[101][101];
     4 int ans=0;;
     5 void prim(int n)
     6 {
     7     int lowcost[101],closet[101],min=0xfffff;
     8     int i,j,k;
     9     int used[101];
    10     memset(used,0,sizeof(used));
    11     for(i=1;i<=n;i++)
    12         lowcost[i]=g[i][1],
    13         //printf("%d
    ",lowcost[i]),
    14         closet[i]=1;
    15     used[1]=1;
    16     for(i=1;i<n;i++)
    17     {
    18         min=0xfffff;
    19         j=1;
    20         for(k=2;k<=n;k++)
    21         {
    22             if(lowcost[k]<min&&(!used[k]))
    23                 min=lowcost[k],
    24                 j=k;
    25         }
    26         used[j]=1;
    27         ans+=g[j][closet[j]];
    28         //printf("(%d %d)",j,closet[j]);
    29         for(k=2;k<=n;k++)
    30         {
    31             if(g[k][j]<=lowcost[k]&&(!used[k]))
    32             {
    33                 lowcost[k]=g[k][j];
    34                 closet[k]=j;
    35             }
    36         }
    37         //printf("
    ");
    38         //for(k=1;k<=n;k++)
    39         //printf("closet[%d]=%d,low=%d used=%d
    ",k,closet[k],lowcost[k],used[k]);
    40     }
    41 }
    42 int main()
    43 {
    44     int n,i,j,m,a,b,x;
    45     while(scanf("%d",&n)!=EOF)
    46     {
    47         ans=0;
    48         for(i=1;i<=n;i++)
    49             for(j=1;j<=n;j++)
    50                 scanf("%d",&g[i][j]);
    51             scanf("%d",&m);
    52             for(i=1;i<=m;i++)
    53                 scanf("%d %d",&a,&b),
    54                 g[a][b]=g[b][a]=0;
    55             prim(n);
    56             printf("%d
    ",ans);
    57     }
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    可以让你少奋斗十年的工作经验 .
    MFC多线程编的可能
    MFC中使用ADO方式连接数据库
    vc 获取当前时间
    mfc EDIT字体颜色
    使用css让div半透明
    -----日积月累-----
    TP框架 ---空控制器和空操作
    TP框架常用配置
    TP框架主要文件夹注释
  • 原文地址:https://www.cnblogs.com/zeze/p/hdoj1102.html
Copyright © 2011-2022 走看看