zoukankan      html  css  js  c++  java
  • Poj2485--Highways

    Highways
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 25002   Accepted: 11533

    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
    RE: 求最大权值。
    Prime:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 const int INF = 0x3f3f3f3f;
     7 int map[550][550], dis[550]; bool vis[550];
     8 int n;
     9 
    10 void Prim()
    11 {
    12     memset(vis, true, sizeof(vis));
    13     for(int i = 1; i <= n; i++)
    14         dis[i] = map[1][i];
    15     vis[1] = false; int  max = -INF;
    16     for(int i = 1; i < n; i++)
    17     {
    18         int temp, min = INF;
    19         for(int j = 1; j <= n; j++)          //寻找吓一条you最小权值边。 
    20         {
    21             if(vis[j] && dis[j] < min)
    22             {
    23                 temp = j;     
    24                 min = dis[j];
    25             }
    26         }
    27         if(min > max)  
    28             max = min;
    29         vis[temp] = false;
    30         for(int j = 1; j <= n; j++)          //更新的是 ? ? 
    31             if(vis[j] && dis[j] > map[temp][j])
    32                 dis[j] = map[temp][j];
    33     }
    34     printf("%d
    ", max);
    35 }
    36 int main()
    37 {
    38     int t;
    39     scanf("%d", &t);
    40     while(t--)
    41     {
    42         scanf("%d", &n);
    43         for(int i = 1; i <= n; i++)
    44             for(int j = 1; j <= n; j++)
    45                 cin >> map[i][j];
    46         Prim();
    47     }
    48     return 0;
    49 }

    Kl ;

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 const int INF = 0x3f3f3f3f;
     9 int map[550][550], father[550];
    10 int n;
    11 struct rode
    12 {
    13     int a, b, w;     
    14 } num[250000];
    15 
    16 bool cmp(rode a, rode b)
    17 {
    18     return a.w < b.w;     
    19 }
    20 
    21 void init()
    22 {
    23     for(int i = 1; i <= n; i++)
    24         father[i] = i;
    25 }
    26 
    27 int find(int a)
    28 {
    29     int r, j , k;
    30     r = a;
    31     while(r != father[r])
    32         r = father[r];
    33     j = a;
    34     while(j != r)
    35     {
    36         k = father[j];
    37         father[j] = r; 
    38         j = k;    
    39     }
    40     return r; 
    41 }
    42 
    43 bool mercy(int a, int b)
    44 {
    45     int q = find(a);
    46     int p = find(b);
    47     if(q != p)
    48     {
    49         father[q] = p;
    50         return true;
    51     }
    52     else
    53         return false;
    54 }
    55 
    56 int main()
    57 {
    58     int t;
    59     while(~scanf("%d", &t))
    60     {
    61         while(t--)
    62         {
    63             int k  = 0;
    64             scanf("%d", &n);
    65             init();
    66             for(int i = 1; i <= n; i++)
    67             for(int j = 1; j <= n; j++)
    68             {
    69                 cin >> map[i][j];
    70                 if(i != j)
    71                 {num[k].a = i; num[k].b = j; num[k++].w = map[i][j];}    
    72             }    
    73             sort(num, num+k, cmp);
    74             int max = -INF;
    75             for(int i = 0; i < k; i++)
    76             {
    77                 if(mercy(num[i].a, num[i].b))    
    78                 {
    79                     if(num[i].w > max)
    80                         max = num[i].w; 
    81                 }
    82             }    
    83             printf("%d
    ", max);
    84         }
    85     }
    86     return 0;    
    87 }
  • 相关阅读:
    [LeetCode] 1092. Shortest Common Supersequence
    [LeetCode] 1091. Shortest Path in Binary Matrix
    [LeetCode] 1090. Largest Values From Labels
    [LeetCode] 1089. Duplicate Zeros
    git log
    Java-Note
    (转载)深入解析String#intern
    Android——LruCache源码解析
    (转载)gcc编译选项总结
    Java——LinkedHashMap源码解析
  • 原文地址:https://www.cnblogs.com/soTired/p/4725485.html
Copyright © 2011-2022 走看看