1. 程式人生 > >POJ 2485 - Highways(求最小生成樹的最大權值-Kruskal演算法)

POJ 2485 - Highways(求最小生成樹的最大權值-Kruskal演算法)

題目

Language:Default Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36414 Accepted: 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:[email protected]

分析
題目大概意思就是,輸入城鎮數量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;
}