POJ 2485-Highways(并查集)

来源:互联网 发布:人工智能出现表明 编辑:程序博客网 时间:2024/06/03 21:39
H - Highways
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2485
Appoint description: 

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

130 990 692990 0 179692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.


思路:
   这次不是求最小生成树,而是求最小生成树中的最大边,模板题。



AC代码:

#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<cmath>typedef long long ll;using namespace std;#define T 55000#define inf 0x3f3f3f3fint n,c;struct node{int u,v,val;bool operator<(const node& a)const{return val<a.val;}node(){}node(int _u,int _v,int _val):u(_u),v(_v),val(_val){}}v[T];int par[T];void Init(int n){for(int i=0;i<=n;++i){par[i] = i;}}int find(int x){int xx = x;while(par[xx]!=xx){xx = par[xx];}while(xx!=x){int tmp = par[x];par[x] = xx;x = tmp;}return xx;}int kruskal(){Init(c);int ma = 0,cnt=0;int tx,ty,i;for(i=0;i<c;++i){tx = find(v[i].u);ty = find(v[i].v);if(tx!=ty){cnt++;ma = max(ma,v[i].val);par[tx] = ty;}}return ma;}int main(){#ifdef zsc freopen("input.txt","r",stdin);#endif int N,i,j,k; scanf("%d",&N); while(N--) { scanf("%d",&n); c = 0; for(i=0;i<n;++i){ for(j=0;j<n;++j){   scanf("%d",&k);   if(i<j){   v[c++] = node(i,j,k);   } } } sort(v,v+c); printf("%d\n",kruskal()); }    return 0;}


0 0
原创粉丝点击