zoukankan      html  css  js  c++  java
  • POJ 2485 Highways (kruskal 最小生成树)

    Highways

    POJ 2485
    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

    求连接所有结点的最小生成树 并得出最小生成树求其中最大权值

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,coun;
    struct road
    {
        int f,t;
        int w;
    };
    road vil[25000+10];
    int fa[25000+10];
    int cmp(road a ,road b) {return a.w<b.w;}
    int find(int x)
    {
        return fa[x]==x?x:fa[x]=find(fa[x]);
    }
    int Kruskal()
    {
        int ans=-1;
        for(int i=0;i<coun;i++) fa[i]=i;
        sort(vil,vil+coun,cmp);
    
        for(int i=0;i<coun;i++)
        {
            int x=find(vil[i].f);
            int y=find(vil[i].t);
            if(x!=y)
            {
                fa[x]=y;
                if(vil[i].w>ans) ans=vil[i].w;
            }
        }
        return ans;
    }
    int main()
    {
    
        int i,j;
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            coun=0;
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    int waste;
                    scanf("%d",&waste);
                    if(i<j)
                    {
                        vil[coun].f=i;
                        vil[coun].t=j;
                        vil[coun].w=waste;
                        coun++;
                    }
                }
            }
            int ans=Kruskal();
            printf("%d
    ",ans);
        }
        return  0;
    }
    View Code
  • 相关阅读:
    细说java中Map的两种迭代方式
    Greenplum query Oracle via DLINK
    去除高清视频锯齿几个方法
    c语言基础学习09_关于复合类型的复习
    c语言基础学习09_复合类型
    原码、反码、补码 详解
    Win10系统怎样让图片的打开方式为照片查看器?
    Android内存泄漏分析实战
    grid control 11.1.0.1 安装指南
    Web中树形数据(层级关系数据)的实现—以行政区树为例
  • 原文地址:https://www.cnblogs.com/sola1994/p/4139856.html
Copyright © 2011-2022 走看看