• poj 2485 Highways


    Highways
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23376   Accepted: 10797

    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
     
     
    分析:
    prime算法:
     1 #include<iostream>
     2 #include<queue>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<algorithm>
     7 using namespace std;
     8 #define size 500
     9 #define inf 999999999
    10 int m[size+5][size+5];
    11 int lowcost[size+5];
    12 /*struct cmp{
    13     operator bool(int a,int b){
    14     return lowcost[a]> lowcost[b];                                      
    15 }
    16 }; */
    17 void prime(int n){
    18     int i=0,j;
    19     int k=0;
    20     //priority_queue<int, vector<int>,cmp> q;    
    21     lowcost[0]=0; 
    22     for(i=1;i<n;i++){
    23         lowcost[i]=m[0][i];
    24         //q.push(lowcost[i]);
    25     }                
    26     int max=0;          
    27     for(i=1;i<n;i++){
    28         int min=inf;
    29         for(j=1;j<n;j++){
    30             if(lowcost[j]&&lowcost[j]<min){
    31                 min=lowcost[j];
    32                 k=j;
    33             }
    34         }
    35         if(min>max){
    36             max=min;
    37         }
    38         //cout<<"i:  "<<lowcost[i]<<endl;
    39         for(j=1;j<n;j++){
    40             if(lowcost[j]>m[k][j]){
    41                 lowcost[j]=m[k][j];
    42             }
    43         }
    44         lowcost[k]=0;
    45     }
    46     /*for(i=0;i<n;i++)
    47     cout<<lowcost[i]<<endl;*/
    48     cout<<max<<endl;
    49 }
    50 int main(){
    51     int n;
    52     scanf("%d",&n);
    53     while(n--){
    54         int i,j,s;
    55         scanf("%d",&s);
    56         for(i=0;i<s;i++){
    57             for(j=0;j<s;j++){
    58                 scanf("%d",&m[i][j]);
    59             }
    60         }
    61         prime(s);
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    css3中-moz、-ms、-webkit 是什么意思
    自定义AppServer
    自定义AppSession
    分离Command
    创建简单的Telnet实例
    注册表权限设置
    centos root登录password 忘记解决的方法
    ajaxFileUpload+struts2实现多文件上传
    计算机图形学(二)输出图元_6_OpenGL曲线函数_2_中点画圆算法
    linux命令的别名alias,unalias
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4288754.html
Copyright © 2020-2023  润新知