2017-03- 14 POJ 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.

Flatopia非常平坦,不幸的是,Flatopia却没有公共的高速公路,所以在岛上的交通很困难。Flatopia政府也意识到了这个问题,所以正在计划建立高速公路,使居民可以随意地往来于各个城镇之间而不用离开高速公路。Flatopia的城镇从1到N编号,每条高速公路连接两个城镇。每条高速公路都是直线,并且可以双向通行。高速公路可以互相交叉,但是司机只能在高速公路的末端,即城镇,转到其他的高速公路。Flatopia政府想使最长的那条公路的长度最小化,但是也要保证每两个城镇之间都能够通过高速公路到达。

【输入】
数据的第一行是一个整数T,表示有T组测试数据。每组数据的第一行是一个整数N(3≤N≤500),表示城镇的个数。接下来的N行,其中第i行包含N个整数,这N个整数其中的第j个整数表示i号城镇到j号城镇的距离,这个距离的大小范围是[1, 65536],每组数据后面有一个空行。
【输出】
对于每组数据,输出一行包含一个整数,表示最长的那条高速公路的长度,使得所有的城镇都能通过高速公路到达,并且这条最长的公路的长度是最小的。
样例输入:1
3
0 990 692
990 0 179
692 179 0
样例输出: 692

Solution

题目大意:就是一个岛国没有高速公路,政府想修高速公路,但钱的问题就是想只修几条公路,但是要联通所有的村庄,问你怎么修,该修哪几条路,求出这几条路中最长的那条,其实就是求最小生成树中最大的那条边。
用Prim算法和Kruskal算法均可以。
prim模板题差不多的,只要在生成每一个点时比较一下边的大小就好了啊

Code

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
#define N 501
using namespace std;
int sq[N][N];
int casenum,n;
int prim()
  {
    int minn[N];
    bool pd[N];
    int MINN=0/*,pos*/;
    memset(minn,0x7f,sizeof(minn));
    minn[1]=0;
    memset(pd,true,sizeof(pd));
    for(int i=1;i<=n;i++)
      {
    int k=0;
    int MInn=0x7f;
    for(int j=1;j<=n;j++)
      /* if(pd[j]&&MInn>minn[j])
         MInn=minn[j],k=j;*/
       if(pd[j]&&(minn[j]<minn[k]))
         k=j,MInn=minn[k];//就这里和下面比大小的地方比prim模板多,是为了比较记录和每一条边的大小
    pd[k]=false;
    for(int j=1;j<=n;j++)
      if(pd[j]&&(sq[k][j]<minn[j]))
        minn[j]=sq[k][j];
    if(MINN<MInn)
      MINN=MInn;
      }
    return MINN;
  }
int main()
{
  scanf("%d",&casenum);
  while(casenum--)
    {
      scanf("%d",&n);
      for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
      scanf("%d",&sq[i][j]);
      printf("%d\n",prim());
    }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值