POJ 2485 Highways

题目链接:

http://poj.org/problem?id=2485

解题思路:

英语不好,果然是硬伤。。。

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.原来这句话的意思时求最小生成树中最长的一个边。。。

只要在求最小生成树的同时,做一下标记就行了。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0xfffffff
using namespace std;

int n;
int edge[505][505];
int vis[505];
int dis[505];
int maxn;

int prime(int cur)
{
    int i, j, tmp;
    maxn = 0;
    memset(vis, 0, sizeof(vis));
    vis[cur] = 1;
    for(i = 1; i <= n; i++)
        dis[i] = edge[cur][i];
    for(i = 2; i <= n; i++)
    {
        int Min = INF;
        for(j = 1; j <= n; j++)
        {
            if(!vis[j] && dis[j] < Min)
                Min = dis[tmp = j];
        }
        vis[tmp] = 1;
        maxn = max(maxn,Min);
        for(j = 1; j <= n; j++)
        {
            if(!vis[j] && dis[j] > edge[tmp][j])
                dis[j] = edge[tmp][j];
        }
    }
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                scanf("%d",&edge[i][j]);
        prime(1);
        printf("%d\n",maxn);
    }
    return 0;
}


你可能感兴趣的:(最小生成树)