poj2485kruskal

Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40303   Accepted: 17615

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

 

/*#include<bits/stdc++.h>*/
//kruskal模板+并查集 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int num=510; int S[num];//并查集 struct Edge{ int v1,v2,w;//边的两个顶点,权重 } edge[num*num];//定义边 bool cmp(Edge a,Edge b) { return a.w<b.w;//按边的权重由小到大排序; } int find(int u) {//查询并查集,返回u的根结点 return S[u]==u?u:find(S[u]); } int n,m;//点,边 ll kruskal() { ll max=0; for(int i=1;i<=n;i++) S[i]=i;//并查集初始化 sort(edge+1,edge+1+m,cmp);//把边按权重由小到大排序 for(int i=1;i<=m;i++){ int a=find(edge[i].v1);//找到根节点 int b=find(edge[i].v2); if(a==b) continue; S[b]=a;//合并 if(max<edge[i].w) max=edge[i].w; } return max; } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d",&n); m=0; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int w; scanf("%d",&w); if(j>i){ m++; edge[m].v1=i,edge[m].v2=j,edge[m].w=w; } } } printf("%lld\n",kruskal()); } return 0; }

 

 

 

上一篇:constructor 属性


下一篇:angularjs学习第四天笔记(第一篇:简单的表单验证)