文档章节

sicily 1090 Highways

Ciel
 Ciel
发布于 2013/01/14 10:30
字数 565
阅读 132
收藏 0

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 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. 

Output

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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

分析:

本题旨在求最小生成树中权值最大的边,套用最小生成树算法,使用变量存储边的值即可。需注意,本体可能出现边非常稠密的状况,权衡判断还是Prim算法更好,Kruskal更适合边稀疏的图。

代码:

// Problem#: 1090
// Submission#: 1892154
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
using namespace std;

#define MAX 500
#define INF 65536
unsigned int dist[MAX][MAX];
unsigned int tmp[MAX];

inline int min( int n ){
    unsigned int min = INF;
    int re;
    for( int i=0 ; i<n ; i++ ){
        if( tmp[i]>0  && tmp[i]<min ){
            re = i;
            min = tmp[i];
        }
    }
    return re;
}

int mst_prim( int n ){
    unsigned int re = 0;
    int k;
    for( int i=0 ; i<n ; i++ )
        tmp[i] = dist[0][i];
    for( int i=1 ; i<n ; i++ ){
        k = min(n);
        re = tmp[k]>re ? tmp[k] : re;
        tmp[k] = 0;
        for( int j=0 ; j<n ; j++ )
            if( dist[k][j] < tmp[j] )
                tmp[j] = dist[k][j];
    }
    return re;
}

int main(){
    int t,n;
    cin >> t;
    while(t--){
        cin >> n;
        for( int i=0 ; i<n ; i++ )
            for( int j=0 ; j<n ; j++ )
                cin >> dist[i][j];

        cout << mst_prim(n) << endl;
        if( t ) cout << endl;
    }
    return 0;
}

© 著作权归作者所有

Ciel
粉丝 3
博文 24
码字总数 18532
作品 0
程序员
私信 提问
Oracle 11g R2 ADG 搭建

--============Oracle ADG搭建============== --==========准备阶段========= 1.检查primary为archivelog模式。 select log_mode from v$database; 如果为noarchivelog模式,切换到archivelo......

UltraSQL
2018/07/23
0
0
PAT 1013 Battle Over Cities(DFS统计连通图的个数)

版权声明:转载请告知博主并要注明出处嗷~ https://blog.csdn.net/AkatsukiItachi/article/details/83817768 PAT1013 1013 Battle Over Cities (25 分) It is vitally important to have ......

语海与冰
2018/11/07
0
0
PTA (Advanced Level)1013 Battle Over Cities

Battle Over Cities   It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city......

suvvm
2018/12/16
0
0
PAT (Advanced Level) 1013. Battle Over Cities (25) DFS求连通分量

题目链接 1013. Battle Over Cities (25) Time limit:400 ms Memory limit:65536 kB Problem Descrpition It is vitally important to have all the cities connected by highways in a war.......

xp731574722
2018/04/25
0
0
oracle查询数据重复

SQL 1: select t4.pre_operaction_id from T_CT_LINK_OPER_EXE_CONDITION t4 left join T_CT_REF_LINK_OPERACTION t5 on t4.link_operaction_id = t5.link_operaction_id where t5.link_id =......

北方蛮子
2013/01/08
489
2

没有更多内容

加载失败,请刷新页面

加载更多

友元函数强制转换

友元函数强制转换 p522

天王盖地虎626
昨天
4
0
js中实现页面跳转(返回前一页、后一页)

本文转载于:专业的前端网站➸js中实现页面跳转(返回前一页、后一页) 一:JS 重载页面,本地刷新,返回上一页 复制代码代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a h...

前端老手
昨天
3
0
JAVA 利用时间戳来判断TOKEN是否过期

import java.time.Instant;import java.time.LocalDateTime;import java.time.ZoneId;import java.time.ZoneOffset;import java.time.format.DateTimeFormatter;/** * @descri......

huangkejie
昨天
3
0
分页查询

一、配置 /*** @author beth* @data 2019-10-14 20:01*/@Configurationpublic class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor(){ ......

一个yuanbeth
昨天
6
0
在LINQPad中使用Ignite.NET

LINQPad是进行.NET开发的一款优秀工具,非常有利于Ignite.NET API的快速入门。 入门 下载LINQPad:linqpad.net/Download.aspx,注意要选择64位操作系统的AnyCPU版本; 安装Ignite.NET的NuGet...

李玉珏
昨天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部