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 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.
Output
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
这题是说在构造最小生成树的时候,找到最大的权边of MST,所以代码如下所示:
#include<stdio.h>
#include <iostream>
#include<string.h>
#define maxn 505
#define inf 65537
int n,m;
int edge[maxn][maxn];
int lowcost[maxn];
int nearvex[maxn];
using namespace std;
void prim(int u0)
{
int sumweight=0;
int max=0;
int i,j;
for(i=1;i<=n;i++)//顶点是从1开始
{
lowcost[i]=edge[u0][i];
nearvex[i]=u0;
}
lowcost[u0]=0;
nearvex[u0]=-1;
for(i=1;i<n;i++)//将n-1个顶点加入到顶点集合T2
{
int min=inf;
int v=-1;
for(j=1;j<=n;j++)
{
if(nearvex[j]!=-1&&lowcost[j]<min)
{
min=lowcost[j];
v=j;
}
}
if(v!=-1)
{
// printf("%d %d %d\n",nearvex[v],v,lowcost[v]);
nearvex[v]=-1;
sumweight+=lowcost[v];
if(max<lowcost[v])
max=lowcost[v];
for(j=1;j<=n;j++)
{
if(nearvex[j]!=-1&&edge[v][j]<lowcost[j])//j在T1集合中&&满足v到j的距离小于j到T2中顶点的最小值
{
lowcost[j]=edge[v][j];
nearvex[j]=v;
}
}
}
}
// printf("sumweight of the mst is %d\n",sumweight);
cout<<max<<endl;
}
int main()
{
int i,j;
int casen;
cin>>casen;
int k=0;
while(casen--)
{
scanf("%d",&n);
m=n;
memset(edge,0,sizeof(edge));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>edge[i][j];
if(i==j)
edge[i][j]=0;
else if(edge[i][j]==0)
edge[i][j]=inf;
}
}
if(k!=0)
cout<<endl;
prim(1);//从顶点1中构造最小生成树
k++;
}
return 0;
}