Description
Input
Output
Sample Input
1
3
0 990 692
990 0 179
692 179 0
Sample Output
692
#include <cstdio>
#include <cstdlib>
#define MAXN 501
typedef struct _E
{
int x, y;
int w;
}E;
int s[MAXN];
int comp(const void *a, const void *b)
{
return ((E*)a)->w - ((E*)b)->w;
}
void Init()
{
for (int i = 0; i < MAXN; i++)
s[i] = -1;
}
int Find(int x)
{
if (s[x] < 0) return x;
else
return s[x] = Find(s[x]);
}
void Union(int r1, int r2)
{
if (s[r1] < s[r2])
s[r2] = r1;
else if (s[r1] == s[r2])
{
s[r2]--;
s[r1] = r2;
}
else
s[r1] = r2;
}
int Kruskal(E e[], int K)
{
int i, ret = 0, xr, yr;
for (i = 0; i < K; i++)
{
xr = Find(e[i].x);
yr = Find(e[i].y);
if (xr != yr)
{
Union(xr, yr);
ret = e[i].w > ret ? e[i].w : ret;
}
}
return ret;
}
int main()
{
int T, N, i, j, k, map[MAXN][MAXN];
E e[MAXN * (MAXN - 1) / 2];
scanf("%d", &T);
while (T-- > 0)
{
scanf("%d", &N);
for (i = 1; i <= N; i++)
for (j = 1; j<= N; j++)
scanf("%d", &map[i][j]);
k = 0;
for (i = 1; i <= N; i++)
for (j = 1; j <= i; j++)
if (i != j)
{
e[k].x = i;
e[k].y = j;
e[k].w = map[i][j];
k++;
}
qsort(e, k, sizeof(E), comp);
Init();
printf("%d/n", Kruskal(e, k));
}
return 0;
}