首页 > 其他 > 详细

BZOJ 1083: [SCOI2005]繁忙的都市 裸的最小生成树

时间:2016-05-31 12:07:13      阅读:294      评论:0      收藏:0      [点我收藏+]

题目链接:

http://www.lydsy.com/JudgeOnline/problem.php?id=1083

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 333;
const int maxm = maxn*maxn;

struct Edge {
    int u, v, w;
    bool operator < (const Edge& tmp) const {
        return w < tmp.w;
    }
}egs[maxm];

int n, m;

int fat[maxn];
int Find(int x) {
    return fat[x] = fat[x] == x ? x : Find(fat[x]);
}

int main() {
    while (scanf("%d%d", &n, &m) == 2) {
        for (int i = 0; i <= n; i++) fat[i] = i;
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d", &egs[i].u, &egs[i].v,&egs[i].w);
        }
        sort(egs, egs + m);
        int ma = -1;
        for (int i = 0; i < m; i++) {
            int pu = Find(egs[i].u);
            int pv = Find(egs[i].v);
            if (pu != pv) {
                ma = max(ma, egs[i].w);
                fat[pv] = pu;
            }
        }
        printf("%d %d\n", n - 1, ma);
    }
    return 0;
}

 

BZOJ 1083: [SCOI2005]繁忙的都市 裸的最小生成树

原文:http://www.cnblogs.com/fenice/p/5545351.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!