ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

poj2485kruskal

2019-11-24 19:01:39  阅读:313  来源: 互联网

标签:692 num highways edge poj2485kruskal towns highway


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; }

 

 

 

标签:692,num,highways,edge,poj2485kruskal,towns,highway
来源: https://www.cnblogs.com/littleyyhome/p/11923705.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有