zoukankan      html  css  js  c++  java
  • POJ 2485

    题目

    Language:Default
    Highways
    Time Limit: 1000MSMemory Limit: 65536K
    Total Submissions: 36414Accepted: 16290

    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.

    Source

    POJ Contest,Author:Mathematica@ZSU

    分析
    题目大概意思就是,输入城镇数量n,然后接下来是一个n×n的邻接矩阵,值就代表两点间的距离,输出最小生成树的最大权值。
    知道这点就好办啦,构建一个结构体,把每组起点、终点和两点间的距离都存进去,然后用Kruskal算法求出最小生成树,把每一条边的权值都存入到一个新的数组中,然后排序输出最大值即可。

    注意
    1、正常输出即可,不要特意去增加换行符
    2、由于输入的权值可能会非常大,所以用cin会超时,我就在这奉献了一次TLE(Time Limit Exceeded)。
    cin和scanf的区别是scanf是格式化输入,printf是格式化输出,效率较高; cin是输入流,cout是输出流,效率稍低。cin与cout之所以效率低,因为是先把要输入/出的东西存入缓冲区,再输入/出,导致效率降低。 详情可参见大佬的一篇博客:https://www.cnblogs.com/limera/p/5405705.html

    最后上代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAXN=1e5 + 10;
    
    struct node
    {
        int u,v;//u:起点,v:终点 
        int dis;//两点距离 
    }N[MAXN];
    
    int pre[MAXN];
    int n;
    int find(int x)
    {
        if(x==pre[x]) return x;
        return pre[x]=find(pre[x]);
    }
    bool cmp1(node a,node b)
    {
        return a.dis < b.dis;
    }
    bool cmp2(int a,int b)
    {
        return a>b;
    }
    void init()
    {
        for(int i=0;i<=n;i++)
        {
            pre[i]=i;
        }
    }
    
    int main()
    {
        int t,i,k,j;
        int a[MAXN];
        cin>>t;
        while(t--)
        {
            memset(N,0,sizeof(N));
            memset(a,0,sizeof(a));
            k=0;
            cin>>n;
            init();
            for(i=0;i<n;i++)//将每条路径的起点、终点和距离都存入到结构体中 
            {
                for(j=0;j<n;j++)
                {
                    N[k].u=i;//起点 
                    N[k].v=j;//终点 
                    //cin>>N[k].dis;//如果输入数据过大,这样会超时 
                    scanf("%d",&N[k].dis);//两点距离 
                    k++;
                }
            }
            sort(N,N+k,cmp1);
            j=0;
            for(i=0;i<k;i++)
            {
                int dx=find(N[i].u);
                int dy=find(N[i].v);
                if(dy!=dx)
                {
                    a[j++]=N[i].dis;
                    pre[dy]=dx;
                }
                if(j==n-1) break;//最小生成树的条件:边数=顶点数-1 
            }
            sort(a,a+j,cmp2);
            cout<<a[0]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    正则表达式 (记录中...)
    css 坑记
    WebApi 中使用 Token
    WebApi 中使用 Session
    微信小程序 入门
    .net EF监控 MiniProfiler
    css布局
    移动端1像素边框问题
    移动端页面自适应解决方案:rem 布局篇
    js重点知识总结
  • 原文地址:https://www.cnblogs.com/cnlik/p/11851869.html
Copyright © 2011-2022 走看看