Highways
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.
給出n個點分別到每個點的距離 給出的實例中的三行三個數字 分別是第一個點分別到第1 2 3個點的距離 第二個點到第1 . 2 3個點的距離 第三個點到第1 2 3 個點的距離 題目就是這么的迷人
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int f,to,val;
}bian[100001];
int pre[10001];
int find(int p)
{
while(p!=pre[p])
{
p=pre[p];
}
return p;
}
bool cmp(node a,node b)
{
return a.val <b.val ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,shu=0;;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
pre[i]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&m);
bian[shu].f =i;
bian[shu].to =j;
bian[shu].val =m;
shu++;
}
}
sort(bian,bian+shu,cmp);
int ma=0;
for(int i=0;i<shu;i++)
{
int fx=find(bian[i].f );
int fy=find(bian[i].to);
if(fx!=fy)
{
if(ma<bian[i].val )
{
ma=bian[i].val ;
}
pre[fx]=fy;
}
}
printf("%d
",ma);
}
return 0;
}
總結
- 上一篇: python采用那种编程模式_使用Mix
- 下一篇: 禾川触摸屏编程软件_汇川PLC编程PLC