POJ 2485--Highways

題目:

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.


題意: 求最小生成樹中的最大邊。只要在添邊的時候記錄一下就好了。最小生成樹算法見博客  HDU 1233 還是暢通工程


實現:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX = 505;

int p[MAX];
int path[MAX][MAX];

int n;

struct node {
    int v1, v2, e;
    bool friend operator < (node n1, node n2) {
        return n1.e > n2.e;
    }
};

int _find(int x) {
    return p[x] = (p[x] == x? x:_find(p[x]));
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        priority_queue <node> q;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                scanf("%d", &path[i][j]);
            }
        }

        for (int i = 0; i < n; i++)
            p[i] = i;

        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                node head;
                head.v1 = i;
                head.v2 = j;
                head.e = path[i][j];
                q.push(head);
            }
        }
        int ans = -1;
        while(!q.empty()) {
            node tt;
            tt = q.top();
            q.pop();
            int x, y;
            x = _find(tt.v1);
            y = _find(tt.v2);
            if (x != y) {
                p[x] = y;
                if (ans < tt.e)
                    ans = tt.e;//記錄最大的
            }
        }
        printf("%d\n", ans);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章