POJ2485 Kruscal

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;
}

本文转自:http://ljf-cnyali.cn/index.php/archives/127

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值