zoukankan      html  css  js  c++  java
  • Highways(求最小生成树的最大边)

    Highways

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 4   Accepted Submission(s) : 1
    Problem 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
     题解:prime;
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 const int INF=0x3f3f3f3f;
     4 const int MAXN=510;
     5 #define MAX(x,y) (x>y?x:y) 
     6 int vis[MAXN],map[MAXN][MAXN],low[MAXN];
     7 int N,answer;
     8 void prime(){
     9     memset(vis,0,sizeof(vis));
    10     int flot=1,temp,k;
    11     answer=-INF;
    12     vis[1]=1;
    13     for(int i=1;i<=N;i++)low[i]=map[1][i];
    14     for(int i=1;i<=N;i++){
    15         temp=INF;
    16         for(int j=1;j<=N;j++)
    17         if(!vis[j]&&temp>low[j])temp=low[k=j];
    18         if(temp==INF){
    19         //    if(flot!=N)ans=0; 
    20         //    printf("flot=%d,N=%d
    ",flot,N);
    21             break;
    22         }
    23         answer=MAX(answer,temp);
    24         vis[k]=1;
    25         flot++;
    26         for(int j=1;j<=N;j++){
    27             if(!vis[j]&&low[j]>map[k][j])low[j]=map[k][j];
    28             }
    29         }
    30     }
    31 int main(){
    32                 int T,a;
    33                 scanf("%d",&T);
    34         while(T--){
    35             memset(map,INF,sizeof(map));
    36                 scanf("%d",&N);
    37                 for(int i=1;i<=N;i++){
    38                     for(int j=1;j<=N;j++){
    39                         scanf("%d",&a);
    40                         if(j>i){
    41                             if(a<map[i][j])map[i][j]=map[j][i]=a;
    42                         }
    43                     }
    44                 }
    45                 prime();
    46                 printf("%d
    ",answer);
    47                     }    
    48             return 0;
    49 }
  • 相关阅读:
    计算机二级-C语言-程序修改题-190114记录-对整型变量进行取余操作可以取得各个位上的值。
    计算机二级C语言选择题错题知识点记录。
    计算机二级-C语言-对文件的读写操作。链表的定义与赋值。对字符串的遍历和处理。
    二十七、Java基础之数组的排列
    二十六、Java语言之二维数组
    二十五、Java基础之一维数组
    二十四、Java基础之自定义异常
    二十三、Java基础之异常及异常处理机制
    二十二、Java基础之内部类
    二十一、Java基础之访问控制权限
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4726711.html
Copyright © 2011-2022 走看看