zoukankan      html  css  js  c++  java
  • POJ 2485 Highways

    Highways

    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.

    题意: 第一行是1->1, 1->2, 1->3的距离,求最小生成树中最大边。

    题解: Kruskal算法,直接比较就好了。

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=510*510;
    struct node {
        int x, y;
        int price ;
    }a[maxn];
    
    int vis[510];
    int  find(int x)
    {
        if(vis[x]==x)
        return x;
        return find(vis[x]);
    }
    
    bool cmp(node a,node b)
    {
        return a.price <b.price ;
    }
    
    void join(int a,int b)
    {
        int x=find(a);
        int y=find(b);
        if(x!=y)
        vis[x]=y;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            int s,k=0;
            for(int i=1;i<=n;i++)
            vis[i]=i;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&s);
                    a[k].x =i;
                    a[k].y =j;
                    a[k++].price =s;
                }
            }
        //  printf("%d
    ",k);
            sort(a,a+k,cmp);
            int dis=0;
            for(int i=0;i<k;i++)
            {
                if(find(a[i].x )!=find(a[i].y ))
                {
                    if(a[i].price>dis)
                     dis=a[i].price ;
                    join(a[i].x ,a[i].y );
                }
            }
            printf("%d
    ",dis);
        }
    }
    
  • 相关阅读:
    Windows10 Docker 安装 dotnet core sdk 超时
    解决 jQuery-datepicker无法弹出日期的问题
    SQL2008 'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT 的用法无效。
    “entities.LastOrDefault()”引发了类型“System.NotSupportedException”的异常
    快速开发平台
    快速设计ComboBox下拉框
    流程设计-流程模式
    流程设计-流程工具
    快速开发一款APP
    SDP开发
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9220258.html
Copyright © 2011-2022 走看看