zoukankan      html  css  js  c++  java
  • Constructing Roads

    Constructing Roads

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


    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 #include<stdio.h>
     2 int map[101][101],p[101],dist[101];
     3 int main()
     4 {
     5     int i,j,k,n,m,a,b,min,len;
     6     while(~scanf("%d",&n))
     7     {
     8          for(i = 0;i < n;i ++)
     9          {
    10             for(j = 0;j < n;j ++)
    11                 scanf("%d",&map[i][j]);
    12          }
    13          scanf("%d",&m);
    14          for(i = 0;i < m;i ++)
    15          {
    16             scanf("%d%d",&a,&b);
    17             map[a-1][b-1] = map[b-1][a-1] = 0;
    18          }
    19          for(i = 0;i < n;i ++)
    20          {
    21             p[i] = 0;
    22             dist[i] = map[0][i];
    23          }
    24          len = dist[0] = 0;
    25          p[0] = 1;
    26          for(i = 1;i < n;i ++)
    27          {
    28             min = 1010;
    29             k = 0;
    30             for(j = 0;j < n;j ++)
    31             {
    32                 if(!p[j]&&dist[j]<min)
    33                 {
    34                     min = dist[j];
    35                     k = j;
    36                 }
    37             }
    38             len += min;
    39             p[k] = 1;
    40             for(j = 0;j < n;j ++)
    41             {
    42                 if(!p[j]&&dist[j]>map[k][j])
    43                     dist[j] = map[k][j];
    44             }
    45         }
    46         printf("%d
    ",len);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    Thinkphp5.0实战开发一------命名空间详解
    软件测试技术实验二
    软件测试技术作业3---PrintPrimes()
    软件测试技术实验一
    Github使用教程(二)------ Github客户端使用方法
    Github使用教程(一)------ 初识Github
    软件测试技术作业2
    软件测试作业1 — 令我印象最深的BUG
    Github网站加载不完全,响应超时,如何解决
    利用puppeteer实现PDF文件导出
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3328974.html
Copyright © 2011-2022 走看看