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 }
  • 相关阅读:
    高阶篇:1.2)材料和工艺的选择
    高阶篇:4.1.1)QFDI(客户需求转换为设计要求)
    高阶篇:4)可靠性设计-总章
    高阶篇:1.5)如何选择更好的概念-Pugh矩阵法
    知识点篇:2)产品结构设计目标的分类
    高阶篇:1.1)竞品(标杆产品)的拆解和分析benchmarking
    支持向量机
    机器学习概述
    HDU_oj_2055 An easy problem
    HDU_oj_2054 A==B ?
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/2997650.html
Copyright © 2011-2022 走看看