poj--2485--Highways

来源:互联网 发布:网络封包抓取 编辑:程序博客网 时间:2024/05/23 16:45

Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 33635 Accepted: 15210

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

130 990 692990 0 179692 179 0

Sample Output

692

题意:

题目给出图的邻接矩阵,让我们求出这个图最小生成树中最长的那个边的长度,可以用克鲁斯卡尔算法,详见代码中的注释

代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int maxn = 510;
int n, m;
long long maxE;
int father[maxn];
struct unit
{
    int from, to;
    long long cost;
} save[maxn * maxn];

bool compare(unit a, unit b) ///根据边权重进行排序
{
    return a.cost < b.cost;
}
int find(int x)             ///找到点所在的集合
{
    if(x == father[x])
        return x;
    return find(father[x]);
}
bool isConnected(int a, int b)   ///判断两个点是否在同一个集合里面
{
    return find(a) == find(b);
}

void mix(int a, int b)
{
    int u = find(a), v = find(b); ///如果两个点不在同一个集合,则进行合并
    if(u != v)
        father[u] = v;
}
void  Kruskal()
{
    sort(save + 1, save + 1 + m, compare);
    for(int i = 1 ; i <= m; i++)
    {
        if(isConnected(save[i].from, save[i].to))
            continue;
        mix(save[i].from, save[i].to);
        maxE = max(maxE, save[i].cost);
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        int i, j;
        for(i = 1; i <= n; i++)
            father[i] = i;

        int a;
        m = 0;
        for(i = 1; i <= n; i++) ///存储邻接矩阵
        {
            for(j = 1; j <= n; j++)
            {
                scanf("%d", &a);
                save[++m].from = i;
                save[m].to = j;
                save[m].cost = a;
            }
        }
        maxE = 0;
        Kruskal();
        printf("%lld\n", maxE);
    }
    return 0;
}




原创粉丝点击