题目来源:POJ 2485
简单题意及思路:
求最小生成树的最大边。
思路依旧是Kruskal+并查集。直接把最后加入的一条边输出即可(因为已经排序)。
ps:思路没什么问题,反倒是输入数据的时候无从下手...
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXN=250000+5;
struct myDistance
{
int src;
int dest;
int len;
}temp[MAXN];
bool cmp(myDistance a,myDistance b)
{
return a.len<b.len;
}
int pre[505];
int findRoot(int x)
{
while(x!=pre[x])
x=pre[x];
return x;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int N;
scanf("%d",&N);
int i,j;
for(i=1;i<=N;++i)
pre[i]=i;
int curIndex=0;
for(i=1;i<=N;++i)
for(j=1;j<=N;++j) //注意输入数据的方式
{
temp[curIndex].src=i;
temp[curIndex].dest=j;
scanf("%d",&temp[curIndex++].len);
}
sort(temp,temp+N*N,cmp);
int cnt=0;
int ans=0;
for(i=0;i<N*N;++i)
{
if(cnt==N-1)
break;
else
{
if(findRoot(temp[i].src)!=findRoot(temp[i].dest))
{
pre[findRoot(temp[i].dest)]=findRoot(temp[i].src);
cnt++;
ans=temp[i].len;
}
}
}
printf("%d\n",ans);
}
return 0;
}