zoukankan      html  css  js  c++  java
  • Highways

     
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 24383   Accepted: 11243

    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.
    注意提示,开始我用的cin,cout输入输出,结果超时,看到提示用的scanf就AC了
     1 #include <stdio.h>
     2 #define INF 65537;
     3 int map[500][500];
     4 int vis[500];
     5 int dis[500];
     6 int n;
     7 int Prim(){
     8     for(int i=0;i<n;i++){
     9         vis[i]=0;
    10         dis[i]=INF;
    11     }
    12     dis[0]=0;
    13     for(int i=0;i<n;i++){
    14         int p;
    15         int mine=INF;
    16         for(int j=0;j<n;j++){
    17             if(!vis[j]&&mine>dis[j]){
    18                 p=j;
    19                 mine=dis[j];
    20             }
    21         }
    22         vis[p]=1;
    23         for(int j=0;j<n;j++){
    24             if(!vis[j]&&dis[j]>map[p][j])
    25                 dis[j]=map[p][j];
    26         }
    27     }
    28     int maxe=0;
    29     for(int i=0;i<n;i++){
    30         if(maxe<dis[i])
    31             maxe=dis[i];
    32     }
    33     return maxe;
    34 }
    35 int main() {
    36 
    37     int num;
    38     scanf("%d",&num);
    39     for(int i=0;i<num;i++){
    40         scanf("%d",&n);
    41         for(int j=0;j<n;j++){
    42             for(int k=0;k<n;k++){
    43                 scanf("%d",&map[j][k]);
    44             }
    45         }
    46         int maxe=Prim();
    47         printf("%d
    ",maxe);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    nginx简单配置
    解决 eclipse出现 Address already in use: bind
    JavaScript 正则表达式学习
    RabbitMQ的介绍与spring整合
    RabbitMQ的安装与客户端的简单实用
    java中的break与continue
    书单
    (七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取
    (六)SpringBoot2.0基础篇- MyBatis、Redis整合(JedisCluster集群连接)
    (五)SpringBoot2.0基础篇- Mybatis与插件生成代码
  • 原文地址:https://www.cnblogs.com/sdxk/p/4649584.html
Copyright © 2011-2022 走看看