zoukankan      html  css  js  c++  java
  • 【SCOI 2005】 繁忙的都市

    【题目链接】

               点击打开链接

    【算法】

               题目描述比较繁琐,但细心观察后,发现其实就是用kruskal算法求最小生成树

    【代码】

              

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 310
    #define MAXM 10000
    
    int i,n,m,ans,sx,sy,sum;
    
    struct info
    {
        int u,v,c;
    } a[MAXM];
    bool cmp(info a,info b) { return a.c < b.c; }
    
    class DisjointSet
    {
        private :
            int fa[MAXN];
        public :
            inline void init(int n)
            {
                int i;
                for (i = 1; i <= n; i++) fa[i] = i;
            }
            inline int get_root(int x)
            {
                if (fa[x] == x) return x;
                fa[x] = get_root(fa[x]);
                return fa[x];
            }
            inline void merge(int x,int y)
            {
                fa[get_root(x)] = get_root(y);    
            }    
    } s;
    
    int main()
    {
        
        scanf("%d%d",&n,&m);
        for (i = 1; i <= m; i++) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].c);
        sort(a+1,a+m+1,cmp);
        s.init(n);
        for (i = 1; i <= m; i++)
        {
            sx = s.get_root(a[i].u);
            sy = s.get_root(a[i].v);
            if (sx != sy) 
            {
                ans = a[i].c;
                if ((++sum) == n - 1) break;
                s.merge(a[i].u,a[i].v);
            }
        }
        
        printf("%d %d
    ",n-1,ans);
            
        return 0;
    }
  • 相关阅读:
    TCP的初始cwnd和ssthresh
    C/C++ main
    PHP Function
    run bin
    PHP
    LAMP
    PHP MATH
    PHP array sort
    inline
    gcc g++
  • 原文地址:https://www.cnblogs.com/evenbao/p/9196343.html
Copyright © 2011-2022 走看看