poj 2485 Highways
2016-09-18 22:50
507 查看
Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28907 Accepted: 13187
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
【分析】
最小生成树裸题,求最长边而已。
【代码】
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28907 Accepted: 13187
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
【分析】
最小生成树裸题,求最长边而已。
【代码】
//poj 1679 The Unique MST #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define fo(i,j,k) for(i=j;i<=k;i++) using namespace std; int father[505]; int n,m,k,T,s,t,ans,top; struct node { int l,r,v; }a[250005]; inline bool comp(const node &p,const node &q) {return p.v<q.v;} inline int find(int x) { if(x!=father[x]) father[x]=find(father[x]); return father[x]; } int main() { int i,j; scanf("%d",&T); while(T--) { scanf("%d",&n); ans=m=0; fo(i,1,n) father[i]=i; fo(i,1,n) fo(j,1,n) { m++; a[m].l=i; a[m].r=j; scanf("%d",&a[m].v); } sort(a+1,a+m+1,comp); fo(i,1,m) if(find(a[i].l)!=find(a[i].r)) { father[find(a[i].l)]=find(a[i].r); ans=a[i].v; } printf("%d\n",ans); } return 0; }
相关文章推荐
- dispatch
- <设计模式15>模板方法模式
- [转]SpringMVC Controller介绍及常用注解
- hdu2031 进制转换
- android控件——ListView
- DOM中的第一个和最后一个子节点
- hdu 5898 odd-even number 2016ACM/ICPC沈阳赛区网络赛1007
- LeetCode #368: Largest Divisible Subset
- C++ 找出迷宫的最短路径
- nyoj 06 喷水装置 贪心
- swift学习第六天:数组
- 20145311 《信息安全系统设计基础》第一周学习总结
- JAVA编程自学不解:为什么打印输出不是孙悟空?
- JAVA基础--JVM原理(二)
- CSS/CSS3 选择器
- mybatis3整合spring4-采用mapper动态代理的方式开发Dao
- 个人简历页面
- 简单mybatis环境搭建 我的第一个mybatis工程
- python学习初步
- JavaEE之九九乘法表