Highways

描述

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.

输入

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.

输出

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

提示

Huge input,scanf is recommended.

题目大意

输入的第一行是整数T,它表示随后进行了多少测试用例。
每种情况的第一行是整数N(3 <= N <= 500),它是村庄的数量。然后是N行,其中第i个包含N个整数,而这N个整数中的第j个是村庄i与村庄j之间的距离(该距离应为[1,65536]之内的整数)。对于每个测试用例,您应该输出包含整数的线,该线是要建造的最长的道路的长度,以使所有村庄都连接起来,并且该值是最小值。

解题思路

一个简单的并查集问题,要注意不要重复的把数据存到数组里面。然后要对数据根据路程从小到大排序。

代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int a,b,s;
}m[125000+5];
int f[500+5];
void intt(int n) 
{
    for(int i=1;i<=n;i++)
        f[i]=i;
}
int find1(int x) //寻找节点的父亲
{
    return f[x]==x?f[x]:f[x]=find1(f[x]);
}
void update(int x,int y) //联通函数
{
    int dx=find1(x);
    int dy=find1(y);
    if(dx!=dy)
    {
        f[dx]=dy;
    }
}
bool cmp(node x,node y)
{
    return x.s<y.s;
}
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       int n;
       scanf("%d",&n);
       intt(n); //初始化
       int d=0;
       int aa;
       for(int i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               scanf("%d",&aa);
               if(j>i) //用来避免重复的数据存入数组
               {
                   m[d].a=i+1;
                   m[d].b=j+1;
                   m[d].s=aa;
                   d++;
               }
           }
       }
       sort(m,m+d,cmp);//根据路程从小到大排序
       int max1=0;
       for(int i=0;i<d;i++)
       {
           if(find1(m[i].a)!=find1(m[i].b)) //两个城市没有直接或间接联通
           {
               update(m[i].a,m[i].b);//将他们联通
               max1=max(max1,m[i].s);//求最大值
           }
       }
       printf("%d\n",max1);

   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qzm777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值