POJ 2485 Highways
2016-08-04 11:53
387 查看
Highways
DescriptionThe 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.
弱没搞懂prime算法 就用了kruskal算法,继续努力 学会prime.
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn=505; struct node { int st,ed,len; }e[maxn*maxn]; int pre[maxn],n,q; bool cmp(node a,node b) { return a.len<b.len; } int find(int p) { while(p!=pre[p]) p=pre[p]; return p; } void merge(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) pre[fy]=fx; } void kruskal() { int a,b,max=0; for(int i=0;i<q;i++) { a=find(e[i].st); b=find(e[i].ed); if(a!=b) { if(e[i].len>max) max=e[i].len; merge(e[i].st,e[i].ed); } } printf("%d\n",max); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) pre[i]=i; q=0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { scanf("%d",&e[q].len); e[q].st=i;e[q].ed=j; q++; } sort(e,e+q,cmp); kruskal(); } }
相关文章推荐
- [leetcode] 318. Maximum Product of Word Lengths
- cas系列(一)--cas单点登录基本原理
- linux常见设备类型及文件系统
- 整数中1出现的次数(从1到n整数中1出现的次数)
- 浅析Laravel5中队列的配置及使用
- ScrollView是否滑动到底部
- 软键盘操控
- Spring 4支持的Java 8新特性一览
- onSaveInstanceState()方法
- Scala学习笔记5 - 特质
- 安卓开发中Theme.AppCompat.Light的解决方法
- 类似微信文本输入框实现,底部评论输入View,随着文字的增加,textView自增长高度
- ios-Pili直播使用和问题总结
- Insert Delete GetRandom O(1)
- Leetcode: 在原矩阵上实现矩阵的旋转(Python)
- cron表达式详解
- poj 3083 Children of the Candy Corn
- 【ExpandTabView】Android 仿大众,美团下拉菜单ExpandTabView
- 浅谈mysql数据库设计性能提高的若干办法
- nodejs服务器anywhere简介