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

    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

    简单最小生成树,模板题- -
    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define N 510
     4 #define INF 0x7f7f7f7
     5 int map[N][N],dis[N],vis[N];
     6 int n;
     7 int prim()
     8 {
     9     int i,j,now,maxx=-1,minn;
    10     memset(dis,0x7f,sizeof(dis));
    11     memset(vis,0,sizeof(vis));
    12     for(i=0;i<n;i++)
    13         dis[i]=map[0][i];
    14     vis[0]=1;
    15     for(i=1;i<n;i++)
    16     {
    17         minn=INF;
    18         for(j=0;j<n;j++)
    19             if(!vis[j]&&dis[j]<minn)
    20             {
    21                 minn=dis[j];
    22                 now=j;
    23             }
    24         vis[now]=1;
    25         maxx=maxx>minn?maxx:minn;
    26         for(j=0;j<n;j++)
    27             if(!vis[j]&&dis[j]>map[now][j])
    28                 dis[j]=map[now][j];
    29     }
    30     return maxx;
    31 }
    32 int main()
    33 {
    34     int t,i,j;
    35     scanf("%d",&t);
    36     while(t--)
    37     {
    38         scanf("%d",&n);
    39         for(i=0;i<n;i++)
    40             for(j=0;j<n;j++)
    41                 scanf("%d",&map[i][j]);
    42         printf("%d\n",prim());
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    iOS--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook等系统服务开发汇总
    iOS-网络爬虫
    iOS-性能优化
    iOS开发——网络实用技术OC篇&网络爬虫-使用青花瓷抓取网络数据
    深入解析Linux内核及其相关架构的依赖关系
    详解Linux系统中的文件名和文件种类以及文件权限
    Linux系统中使用netcat命令的奇技淫巧
    Linux系统下强大的lsof命令使用宝典
    Linux下多线程下载工具MWget和Axel使用介绍
    Linux下针对路由功能配置iptables的方法详解
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/2997650.html
Copyright © 2011-2022 走看看