Highways
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20664 | Accepted: 9565 |
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.
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.
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
题目分析:
北京大学上的一道老题。叫你求出最小生成树的最长边最短。昨天,没什么状态。比赛的时候我负责打代码,结果把队友坑死了,从开始比赛到比赛结束都在调代码。没进好自己的责任,感觉对不起两个队友。
算法分析:
一开始我就没考虑太多的时间复杂度,直接枚举了。结果果断超时了!后来优化了一下,开始的对每条边改成了二分最短的最长路。结果算是过了275s,但是我还是不怎么满意,我看到排行版上有0s的。说明肯定有更好的算法。但是我还没有想到。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 500 + 5;
const int INF = ~0U >> 2;
struct Node{
int x,y,dist;
bool operator<(const Node& a)const{
return dist < a.dist;
}
}node[MAXN*MAXN];
int n,m,k,f[MAXN];
int Find(int x)
{
if(x==f[x])
return f[x];
return f[x] = Find(f[x]);
}
bool Kruscal(int e)
{
int last,cnt = 0;
for(int i = 0;i <= n;++i)
f[i] = i;
for(int i = 0;i < e;++i){
int a = Find(node[i].x),b = Find(node[i].y);
if(f[a]!=b){
cnt++;
f[a] = b;
last = i;
}
if(cnt==n-1)
break;
}
return cnt==n-1;
}
int main()
{
// freopen("Input.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
k = 0;
scanf("%d",&n);
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
scanf("%d",&node[k].dist);
node[k].x = i;
node[k].y = j;
k++;
}
}
int minv = INF;
sort(node,node+k);
int i,L = 0,R = node[k-1].dist;
while(L <= R)
{
int mid = (L+R)>>1;
for(i = 0;i < k;++i){
if(node[i].dist > mid)
break;
}
if(Kruscal(i))
R = mid - 1;
else
L = mid + 1;
}
printf("%d\n",L);
}
return 0;
}