POJ 2485 Highways (水题入门最小生成树)

28 篇文章 0 订阅

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

题目大意
在 1~n 个城镇之间建设公路,求可以把所有城镇连接起来的最小花费,输出其中最长的边的权值。

解题思路

  1. Prim算法: Prim算法是选取一个点作为出发点,在其余点中选取距离该点最近的顶点,与出发点合并为集合,并标记已经添加过的顶点避免重复访问;之后不断往集合中添加距离现有生成树最近的点,扩大现有生成树集合,直至所有的点均被标记,现有生成树集合不再被更新,退出算法。

2.Kruskal算法:Kruskal算法是先把边递增排序,然后由小边至大边进行检索,若加入该点会成环,那么舍弃;否则现有生成树存在顶点数 +1,直至顶点数等于图所有顶点,退出算法。在该算法中,使用并查集来判断两点是否属于同一集合,即是否存在环。

代码实现

//Prim算法

#include <iostream>
#include<cstdio>
using namespace std;
#define maxv 506
#define INF (1<<26)
int edge[maxv][maxv],n;
void prim()
{
    int mincost[maxv];
    bool flag[maxv];
    for(int i=0;i<n;i++)
    {
        mincost[i]=INF;
        flag[i]=false;
    }
    mincost[0]=true;
    int res=0;
    while(true)
    {
        int v=-1;
        for(int u=0;u<n;u++)
        {
            if(!flag[u]&&(v==-1||mincost[u]<mincost[v]))
                v=u;
        }
        if(v==-1)break;
        flag[v]=true;
        res=max(res,mincost[v]);
        for(int u=0;u<n;u++)
            mincost[u]=min(mincost[u],edge[u][v]);
    }
    printf("%d\n",res);
}
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",&edge[i][j]);
        prim();
    }
    return 0;
}

//kruskal算法

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 506
int n;
int pre[maxn];
bool flag[maxn];
struct node
{
    int x,y,l;
} edge[maxn*maxn];
bool cmp(node a,node b)
{
    return a.l<b.l;
}
int find_pre(int x)
{
    int r=x;
    while(pre[r]!=r)
    {
        r=pre[r];
    }
    return r;
}
bool make_union(int a,int b)
{
    int fx=find_pre(a);
    int fy=find_pre(b);
    if(fx!=fy)
    {
        pre[fx]=fy;
        return true;
    }
    return false;
}
int main()
{
    int T,v,len;
    scanf("%d",&T);
    while(T--)
    {
        memset(flag,false,sizeof(flag));
        scanf("%d",&n);
        v=0;
        for(int i=0; i<n; i++)
        {
            pre[i]=i;
            for(int j=0; j<n; j++)
            {
                scanf("%d",&len);
                if(i>j)
                {
                    edge[v].x=i;
                    edge[v].y=j;
                    edge[v].l=len;
                    v++;
                }
            }
        }
        int nowv=0;
        int maxx=0;
        sort(edge,edge+v,cmp);
        for(int i=0; i<v; i++)
        {
            if(make_union(edge[i].x,edge[i].y))
            {
                nowv++;
                if(maxx<edge[i].l)
                    maxx=edge[i].l;
                if(nowv==v-1) break;
            }

        }
        printf("%d\n",maxx);
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值