zoukankan      html  css  js  c++  java
  • poj 2485 Highways

    Highways
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 24326   Accepted: 11229

    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.

    Source

    POJ Contest,Author:Mathematica@ZSU
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <queue>
     5 #include <stack>
     6 #include <iostream>
     7 using namespace std;
     8 #define lengthmax 65537
     9 int map[505][505];
    10 int dis[505];
    11 int main(){
    12     //freopen("D:\INPUT.txt","r",stdin);
    13     int t,n;
    14     scanf("%d",&t);
    15     while(t--){
    16         scanf("%d",&n);
    17         int i,j;
    18         for(i=0;i<n;i++){
    19             dis[i]=lengthmax;
    20             for(j=0;j<n;j++){
    21                 scanf("%d",&map[i][j]);
    22             }
    23         }
    24         int m=1,mink,s=0,min,want=-1;
    25         dis[0]=0;//已经在集合里面
    26         while(m<n){//循环n-1次
    27             min=lengthmax;
    28             for(i=1;i<n;i++){
    29                 if(dis[i]>map[s][i]){//更新
    30                     dis[i]=map[s][i];
    31                 }
    32                 if(dis[i]&&min>dis[i]){
    33                     min=dis[i];
    34                     mink=i;
    35                 }
    36             }
    37             //cout<<mink<<endl;
    38             //cout<<min<<endl;
    39             if(min>want){
    40                 want=min;
    41                 //cout<<want<<endl;
    42             }
    43             dis[mink]=0;
    44             s=mink;
    45             m++;
    46         }
    47         cout<<want<<endl;
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    day14_集合框架1(ArrayList,LinkedList,HashSet)
    day13_String、StringBuffer、StringBuilder
    初识Java_day01
    关于局部内部类访问带有final修饰符的局部变量
    day03,day04_数组,循环(上)
    day09(下)_异常(上)
    day08_多态
    day11_多线程(多线程安全问题)
    day16_集合框架3(HashMap,TreeMap)
    day09(上)_内部类
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4644993.html
Copyright © 2011-2022 走看看