[poj 2485] Highways –求最小生成树中最长边

[poj 2485] Highways –求最小生成树中最长边

poj题目链接:http://poj.org/problem?id=2485

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36285 Accepted: 16234

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

题意解析:

​ 题目直接给了一个以邻接矩阵的图,表明两点间的距离,要求的最小生成树中的最长边,很明显就是板子题。下学期就大三了,之前的省赛还是区域赛里还是平常训练都没有写过最小生成树的题目,只了解一个大概的思路,这次把prim算法实现一下,因为已经给出了邻接矩阵的形式了,所以不考虑kruskal这个基于邻接表的算法了(懒癌,转换麻烦)。

Prim算法(类似dijkstra算法) 考虑点

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 600;
const int INF = 0x3f3f3f3f;
int a[maxn][maxn];
int n;
bool vis[maxn];
int mincost[maxn];//现有顶点集到剩下的顶点的最小距离

int prim() {
    for (int i = 0; i < n; i++) {
        mincost[i] = a[0][i]; //默认以0为起点求,初始化
        vis[i] = false;
    }
    vis[0] = true;
    mincost[0] = 0;
    int ans = 0;
    int maxe = -1;
    while (true) {
        int v = -1;
        //选取剩余顶点集到已有顶点距离最小的顶点
        for (int u = 0; u < n; u++) {
            if (!vis[u] && (v == -1 || mincost[u] < mincost[v]))v = u;
        }
        if (v == -1)break;
        vis[v] = true;
        ans += mincost[v];
        if (mincost[v] > maxe) maxe = mincost[v];//记录最大边
        for (int u = 0; u < n; u++) {
            mincost[u] = min(mincost[u], a[v][u]);
        }
    }
    return maxe;
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {

        scanf("%d", &n);
        for (int i = 0; i< n; i++){
            for (int j = 0; j< n; j++)
                scanf("%d", &a[i][j]);
        }
        printf("%d\n", prim());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ALLconan/article/details/81669003