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.
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.
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.
1 3 0 990 692 990 0 179 692 179 0
692
这道题 说实话,没看懂题目的意思,所以就去看了下博客,看了下别人写的题意(英语真的越来越渣了QAQ),不过在知道意思后其实也挺简单的,就是有点麻烦,这道题的意思其实就是:
给你N个村庄,然后吧每个村庄之间的距离用个二维数组表现出来 拿例子来说就是 1 和1 的距离是0 , 1和2的距离是990, 1和3的距离是692 ,第二行就是 2和1的距离是990
2和2 的距离是0 以此类推 这样应该就理解了 那么 这道题的最后目的其实是让我们求出最优连通方案中 一条最长的距离是多少,这个不好理解 但其实你把例子画一下也就懂了,真的不是文字就表达清楚了QAQ
这道题的难点有两个,第一个是如何把给的数据转化为我们希望得到的数据,还有就是如和求出最长的那条,当然,代码如下,自己体会吧(抽时间把解释补上去QAQ):
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define MAX 10000010
using namespace std;
int y[510][510];
int par[MAX];
int heigh[MAX];
int t;
struct ege{
int u,v,w;
}G[MAX];
void chushihua(int n)
{
for(int i=0;i<=n;i++)
{
par[i]=i;
heigh[i]=0;
}
}
int find(int n)
{
int r=n;
while(par[r]!=r)
r=par[r];
return r;
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return ;
if(heigh[y]>heigh[x])
par[x]=y;
else
{
par[y]=x;
if(heigh[x]==heigh[y])
heigh[x]++;
}
}
bool same(int x,int y)
{
return find(x)==find(y);
}
bool cmp(ege R,ege b)
{
return R.w<b.w;
}
int kruskal()
{
sort(G+1,G+t+1,cmp);
chushihua(t);
int ans=0;
int mk=0;
for(int i=1;i<=t;i++)
{
if(!same(G[i].u,G[i].v))
{
unite(G[i].u,G[i].v);
mk=max(mk,G[i].w);
ans+=G[i].w;
}
}
return mk;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{ int m;
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&y[i][j]);
}
}
t=0;
for(int k=1;k<=m-1;k++)
{
for(int l=k+1;l<=m;l++)
{
t++;
G[t].u=k;
G[t].v=l;
G[t].w=y[k][l];
}
}
int k;
k=kruskal();
printf("%d\n",k);
}
return 0;
}