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

    这题很水,但一直没看见题目说输入多组数据,添上一下子就过了。     

                Constructing Roads

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

    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
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAX 100000
     4 int map[101][101],v[101];
     5 int prim(int n)//最好的prim的模板。
     6 {
     7     int k[510],i,j;
     8     int min,sum=0,flag,count=1;
     9     memset(k,0,sizeof(k));
    10     memset(v,0,sizeof(v));
    11     for(i=1;i<=n;i++)
    12         k[i]=map[1][i];
    13     for(i=2;i<=n;i++)
    14     {
    15         min=MAX;
    16         for(j=2;j<=n;j++)
    17         {
    18             if(min>k[j]&&v[j]==0)
    19             {
    20                 min=k[j];
    21                 flag=j;
    22             }
    23         }
    24         sum+=min;
    25         v[flag]=1;
    26         for(j=1;j<=n;j++)
    27             if(v[j]==0&&k[j]>map[flag][j]&&flag!=j)
    28                 k[j]=map[flag][j];
    29     }
    30   return sum;
    31 }
    32 int main()
    33 {
    34    int n,i,j,q,min,x,y;
    35       while(~scanf("%d",&n))
    36       {
    37       for(i=1;i<=n;i++)
    38             for(j=1;j<=n;j++)
    39             {
    40                 scanf("%d",&map[i][j]);
    41             }
    42             scanf("%d",&q);
    43             while(q--)
    44             {
    45                 scanf("%d%d",&x,&y);
    46                 map[x][y]=map[y][x]=0;
    47             }
    48         min=prim(n);
    49       printf("%d
    ",min);
    50       }
    51    return 0;
    52 }
  • 相关阅读:
    软工实践结对作业第二次
    团队展示
    软件工程结对作业
    软工实践第二次作业
    栈的初步学习
    课程作业四
    作业
    课程作业2
    博客汇总目录
    Mybatis-plus学习笔记,基于springboot
  • 原文地址:https://www.cnblogs.com/cancangood/p/3315716.html
Copyright © 2011-2022 走看看