洛谷P2330 [SCOI2005]繁忙的都市

对题面进行一下分析,本题并不复杂,需要注意的是,要求是一个一个满足的,也就是说最开始你要保证该图联通,然后连边数最小。

 

emmmmm 这不就是生成树吗,再往下看,要实现最大的边长最小化,这不就是求最小生成树上最长边吗

 

接下来就没什么好说的了,直接上代码

 

#include<bits/stdc++.h>
using namespace std;

const int Maxn = 310;
const int MAxn = 50010;

int n, m, fa[Maxn], res;

struct st { 
    int u, v, w;
} edge[MAxn];

int find ( int x ) {
    
    return fa[x] == x ? x : fa[x] = find ( fa[x] );
    
}

inline void add_edge ( int i, int u, int v, int w ) {
    
    edge[i].u = u;
    edge[i].v = v;
    edge[i].w = w;
    
}

inline int comp ( const st & a, const st & b) {
    
    return a.w < b.w;
    
}

inline void kruscal () {
    
    int cnt = 0;
    sort ( edge + 1, edge + 1 + m ,comp );
    for ( int i = 1; i <= m; ++i ) {
        
        int uu = find( edge[i].u );
        int vv = find( edge[i].v );
        if ( uu == vv ) continue;
        fa[uu] = vv;
        if ( ++cnt == n-1 ) {
            res = edge[i].w;
            break;
        }
    }
    
}
int main () {
    scanf ( "%d %d", &n, &m);
    for ( int i = 1; i <= n; ++i ) fa[i] = i;
    for ( int i = 1; i <= m; ++i ) {
        int x, y, z;
        scanf( "%d %d %d", &x, &y, &z);
        add_edge ( i, x, y, z );
    }
    kruscal ();
    printf ( "%d %d", n-1, res );
    return 0;
}

 

posted @ 2018-09-26 19:57  Hadesa  阅读(120)  评论(0编辑  收藏  举报