zoukankan      html  css  js  c++  java
  • Highways(prim & MST)

    Highways
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23421   Accepted: 10826

    Description

    The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

    Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

    The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

    Input

    The first line of input is an integer T, which tells how many test cases followed. 
    The first line of each case is an integer N (3 <= N <= 500), 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, 65536]) between village i and village j. There is an empty line after each test case.

    Output

    For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

    Sample Input

    1
    
    3
    0 990 692
    990 0 179
    692 179 0

    Sample Output

    692
    

    Hint

    Huge input,scanf is recommended.
     1 #include<stdio.h>
     2 #include<string.h>
     3 const int M = 510 , inf = 0x3f3f3f3f ;
     4 int T , n ;
     5 int map[M][M] ;
     6 int d[M] ;
     7 int p[M] ;
     8 
     9 void prim ()
    10 {
    11     for (int i = 1 ; i <= n ; i++) {
    12         d[i] = map[1][i] ;
    13         p[i] = 1 ;
    14     }
    15     d[1] = 0 ;
    16     int ans = -inf ;
    17     for (int i = 1 ; i < n ; i++) {
    18         int minc = inf , k ;
    19         for (int j = 1 ; j <= n ; j++) {
    20             if (d[j] && d[j] < minc) {
    21                 minc = d[j] ;
    22                 k = j ;
    23             }
    24         }
    25             d[k] = 0 ;
    26             for (int j = 1 ; j <= n ; j++) {
    27                 if (d[j] && d[j] > map[k][j]) {
    28                     d[j] = map[k][j] ;
    29                     p[j] = k ;
    30                 }
    31             }
    32             if (ans < minc)
    33                 ans = minc ;
    34           //  printf ("minc=%d
    " , minc) ;
    35     }
    36     printf ("%d
    " , ans) ;
    37 }
    38 
    39 int main ()
    40 {
    41    // freopen ("a.txt" , "r" , stdin) ;
    42     scanf ("%d" , &T) ;
    43     while (T--) {
    44         scanf ("%d" , &n) ;
    45         for (int i = 1 ; i <= n ; i++) {
    46             for (int j = 1 ; j <= n ; j++) {
    47                 scanf ("%d" , &map[i][j]) ;
    48                 if (i == j)
    49                     map[i][j] = inf ;
    50             }
    51         }
    52         prim () ;
    53     }
    54 }
    View Code

    which is the length of longest to be built : 没正确理解,orz

  • 相关阅读:
    Guns 01 项目基本运行
    个人 比较好用的软件
    个人 软件开发 综合技能提升
    开源框架 综合知识
    开源框架 工作流框架
    开源框架 Java 开发框架 1
    开源框架 Java 管理系统
    开源框架 C#
    scp(安全拷贝)和rsync(增量复制)
    完全分布式集群的准备工作
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4312419.html
Copyright © 2011-2022 走看看