zoukankan      html  css  js  c++  java
  • C

    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

    题目意思:和畅通工程差不多,只是要输出最长的那条路:
    解题思路:最小生成树

     1 #include <iostream>
     2 #include <queue>
     3 #include <string.h>
     4 #include <stdio.h>
     5 #include <math.h>
     6 using namespace std;
     7 
     8 const int MAX = 500 + 100;
     9 int visit[MAX];
    10 int n;
    11 
    12 struct S
    13 {
    14     int a,b;
    15     int len;
    16 };
    17 
    18 struct cmp
    19 {
    20     bool operator() ( S a,S b )
    21     {
    22         return a.len>b.len;
    23     }
    24 };
    25 
    26 int Find (int x)
    27 {
    28     if(x == visit[x])
    29         return x;
    30     else
    31         return visit[x] = Find(visit[x]);
    32 }
    33 
    34 int mix(int x,int y)
    35 {
    36     int Tx = Find(x);
    37     int Ty = Find(y);
    38     if(Tx != Ty)
    39     {
    40         visit[Tx] = Ty;
    41         return 1;
    42     }
    43     return 0;
    44 
    45 }
    46 
    47 int main()
    48 {
    49     int N;
    50     cin>>N;
    51 
    52     while(N--)
    53     {
    54         cin>>n;
    55         int Map[MAX][MAX];
    56         for(int i = 1;i <= n;i++)
    57         {
    58             visit[i] = i;
    59             for(int j = 1;j <= n;j++)
    60                 cin>>Map[i][j];
    61         }
    62 
    63         S temp;
    64         priority_queue<S,vector<S>,cmp>P;
    65         for(int i = 1;i < n;i++)
    66             for(int j = i+1;j <=n;j++)
    67             {
    68                temp.a = i;temp.b = j;
    69                temp.len = Map[i][j];
    70                P.push(temp);
    71             }
    72 
    73         int Max = 0;
    74         while(!P.empty())
    75         {
    76             temp = P.top();
    77             P.pop();
    78 
    79             if( mix(temp.a,temp.b)==1)
    80             {
    81                 Max = Max > temp.len?Max:temp.len;
    82             }
    83         }
    84 
    85         cout<<Max<<endl;
    86     }
    87 
    88     return 0;
    89 }





  • 相关阅读:
    切片/修改
    代码块,-- 循环结构--字符串的格式化--字符串相关函数功能
    format 填充符号 与 格式化
    字符串的格式化format
    python字符串相关函数 *title *upper *lower *swapcase *len *count *find *index *starts with *endswith *isalpha *isdecimal *split *center *strip *replace
    双层循环经典小项目题
    while 小项目练习
    for循环 | range 对象
    关键字的使用 pass break continue
    python字符串的切片
  • 原文地址:https://www.cnblogs.com/a2985812043/p/7294578.html
Copyright © 2011-2022 走看看