关闭

POJ 2485-Highways(最小生成树裸题-prim/kruskal)

标签: POJ 2485Highways最小生成树primkruskal
714人阅读 评论(0) 收藏 举报
分类:

Highways

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27781   Accepted: 12690

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.

题目意思:

有N个城市,用邻接矩阵给出两两之间的距离。
求最小生成树中的最长公路。
题目中给出的是一个邻接矩阵。

解题思路:

最小生成树裸题。
思想:从第1个顶点开始,添加与之有边的点;再从这些已添加的边中选择最小权值的继续添加。
用prim/kruskal模板改改就能A。


①prim
/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:prim.cpp
* 作    者:单昕昕
* 完成日期:2016年6月1日
* 版 本 号:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1010
#define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],mincost[MAXN],n,t;
bool used[MAXN];//标识是否使用过

void prim()
{
    fill(mincost,mincost+n,INF);
    fill(used,used+n,false);
    mincost[0]=0;
    int res=0,Max=-1;
    while(true)
    {
        int v=-1;
        for(int u=0; u<n; ++u)
        {//从不属于已加入生成树的顶点中选取从已加入生成树的点到该顶点的权值最小的点
            if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
                v=u;
        }
        if(v==-1) break;
        used[v]=true;
        Max=max(Max,mincost[v]);
        //res+=mincost[v];
        for(int u=0; u<n; ++u)
            mincost[u]=min(mincost[u],cost[v][u]);
    }
    cout<<Max<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        /*for(int i=0; i<n; ++i)
            for(int j=0; j<n; ++j)
                cost[i][j]=INF;*/
        for(int i=0; i<n; i++)
            for(int j=0; j<n; ++j)
                cin>>cost[i][j];
        prim();
    }
    return 0;
}
/*
1

3
0 990 692
990 0 179
692 179 0
*/

②kruskal

/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:prim.cpp
* 作    者:单昕昕
* 完成日期:2016年3月30日
* 版 本 号:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 10010
#define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],pre[MAXN],n,cnt;
bool used[MAXN];//标识是否使用过

struct edge
{
    int u,v,cost;
};
bool cmp(const edge &e1,const edge &e2)
{
    return e1.cost<e2.cost;
}
edge es[MAXN];
int V,E;//顶点数和边数

int set_find(int x)
{
    while(x!=pre[x])
        x=pre[x];
    return x;
}
void unite(int p,int q)
{
    p=set_find(p);
    q=set_find(q);
    if(p!=q) pre[p]=q;
}
bool same(int x,int y)
{
    return set_find(x)==set_find(y);
}
void init_union_find(int n)
{
    for(int i=0; i<=n; i++)
        pre[i]=i;
}
void kruskal()
{
    sort(es,es+cnt,cmp);//按照edge.cost的顺序升序排列
    init_union_find(V);//并查集初始化
    //int res=0;
    int Max=-1;
    for(int i=0; i<E; ++i)
    {
        edge e=es[i];
        if(!same(e.u,e.v))
        {
            unite(e.u,e.v);
            //res+=e.cost;
            Max=max(Max,e.cost);
        }
    }
    cout<<Max<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        cnt=0;
        /*for(int i=0; i<n; ++i)
            for(int j=0; j<n; ++j)
                cost[i][j]=INF;*/
        for(int i=0; i<n; i++)
            for(int j=0; j<n; ++j)
            {
                es[cnt].u=i;
                es[cnt].v=j;
                cin>>es[cnt++].cost;
            }
        V=n;
        E=n*n;
        kruskal();
    }
    return 0;
}
/*
1

3
0 990 692
990 0 179
692 179 0
*/


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:439774次
    • 积分:13707
    • 等级:
    • 排名:第974名
    • 原创:906篇
    • 转载:20篇
    • 译文:0篇
    • 评论:427条
    友情链接