2017年4月3日 | ljfcnyali
题目大意
给你一个邻接矩阵,求最小生成树的最大边
Sample Input
1
3
0 990 692
990 0 179
692 179 0
Sample Output
692
题目分析
Kruscal水过,不讲了。
AC代码
/*************************************************************************
> File Name: POJ2485.cpp
> Author: ljf-cnyali
> Mail: ljfcnyali@gmail.com
> Created Time: 2017/4/3 13:43:12
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)
const int maxn = 550;
int n, m;
struct node {
int x, y, w;
}Edge[maxn * maxn];
int fa[maxn];
int cmp(node x, node y) {
return x.w < y.w;
}
int cha(int x) {
return x == fa[x] ? x : fa[x] = cha(fa[x]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T --) {
scanf("%d", &m);
int w;
n = 0;
REP(i, 1, m) {
fa[i] = i;
REP(j, 1, m)
if(scanf("%d", &w) && i < j) {
Edge[++ n].x = i;
Edge[n].y = j;
Edge[n].w = w;
}
}
sort(Edge + 1, Edge + n + 1, cmp);
int ans = 0, sum = 0;
REP(i, 1, n) {
int fx = cha(Edge[i].x);
int fy = cha(Edge[i].y);
if(fx != fy) {
fa[fx] = fy;
ans = max(ans, Edge[i].w);
if(++ sum == m - 1)
break ;
}
}
printf("%d\n", ans);
}
return 0;
}