• 洛谷 P4180 [BJWC2010]严格次小生成树


    传送门

    AcWing 356 次小生成树

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const int maxn(1e6 + 10);
    const int maxm(3e6 + 10);
    int ecnt, head[maxn];
    int pre[maxn], dep[maxn];
    int f[maxn][17], d1[maxn][17], d2[maxn][17];
    
    struct edge {
        int u, v, w;
        bool used;
        
        bool operator <(const edge& rhs) const
        {
            return w < rhs.w;
        }
    } edges[maxm];
    
    struct _edge {
        int to, wt, nxt;
    } _edges[maxm];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    void addEdge(int u, int v, int w)
    {
        _edges[ecnt].to = v;
        _edges[ecnt].wt = w;
        _edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    int Find(int x)
    {
        return pre[x] == x ? x : pre[x] = Find(pre[x]);
    }
    
    void Union(int u, int v)
    {
        pre[Find(v)] = Find(u);
    }
    
    ll kruskal(int n, int m)
    {
        for (int i = 1; i <= n; ++i) {
            pre[i] = i;
        }
        sort(edges, edges + m);
        ll res = 0;
        for (int i = 0; i < m; ++i) {
            int u = edges[i].u, v = edges[i].v, w = edges[i].w;
            if (Find(u) != Find(v)) {
                Union(u, v);
                res += w;
                edges[i].used = true;
            }
        }
        return res;
    }
    
    void build(int m)
    {
        memset(head, -1, sizeof head);
        for (int i = 0; i < m; ++i) {
            if (edges[i].used) {
                int u = edges[i].u, v = edges[i].v, w = edges[i].w;
                addEdge(u, v, w);
                addEdge(v, u, w);
            }
        }
    }
    
    void bfs(int root)
    {
        dep[root] = 1;
        queue<int> q;
        q.push(root);
        while (not q.empty()) {
            int u = q.front();
            q.pop();
            for (int i = head[u]; compl i; i = _edges[i].nxt) {
                int v = _edges[i].to, w = _edges[i].wt;
                if (dep[v]) {
                    continue;
                }
                dep[v] = dep[u] + 1;
                q.push(v);
                f[v][0] = u;
                d1[v][0] = w;
                d2[v][0] = -inf;
                for (int j = 1; j < 17; ++j) {
                    int anc = f[v][j - 1];
                    f[v][j] = f[anc][j - 1];
                    int dis[4] = { d1[v][j - 1], d2[v][j - 1], d1[anc][j - 1], d2[anc][j - 1] };
                    d1[v][j] = d2[v][j] = -inf;
                    for (int k = 0; k < 4; ++k) {
                        int d = dis[k];
                        if (d > d1[v][j]) {
                            d2[v][j] = d1[v][j];
                            d1[v][j] = d;
                        } else if (d not_eq d1[v][j] and d > d2[v][j]) {
                            d2[v][j] = d;
                        }
                    }
                }
            }
        }
    }
    
    int lca(int u, int v, int w)
    {
        static int dis[maxn * 2];
        int cnt = 0;
        if (dep[u] < dep[v]) {
            swap(u, v);
        }
        for (int i = 16; i >= 0; --i) {
            if (dep[f[u][i]] >= dep[v]) {
                dis[cnt++] = d1[u][i];
                dis[cnt++] = d2[u][i];
                u = f[u][i];
            }
        }
        if (u not_eq v) {
            for (int i = 16; i >= 0; --i) {
                if (f[u][i] not_eq f[v][i]) {
                    dis[cnt++] = d1[u][i];
                    dis[cnt++] = d2[u][i];
                    dis[cnt++] = d1[v][i];
                    dis[cnt++] = d2[v][i];
                    u = f[u][i];
                    v = f[v][i];
                }
            }
            dis[cnt++] = d1[u][0];
            dis[cnt++] = d1[v][0];
        }
        int dis1 = -inf, dis2 = -inf;
        for (int i = 0; i < cnt; ++i) {
            int d = dis[i];
            if (d > dis1) {
                dis2 = dis1;
                dis1 = d;
            } else if (d not_eq dis1 and d > dis2) {
                dis2 = d;
            }
        }
        if (w > dis1) {
            return w - dis1;
        }
        if (w > dis2) {
            return w - dis2;
        }
        return inf;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        int n = read(), m = read();
        for (int i = 0; i < m; ++i) {
            int u = read(), v = read(), w = read();
            addEdge(u, v, w);
            edges[i] = edge{ u, v, w };
        }
        ll sum = kruskal(n, m);
        build(m);
        bfs(1);
        ll res = 1e18;
        for (int i = 0; i < m; ++i) {
            if (not edges[i].used) {
                int u = edges[i].u, v = edges[i].v, w = edges[i].w;
                res = min(res, sum + lca(u, v, w));
            }
        }
        write(res, true);
        return 0;
    }
    
  • 相关阅读:
    C# 获取类似java gettime() 的时间格式
    LUbuntu电脑棒安装指南
    Visual Studio Gallery
    SQL SERVER 分页存储过程
    asp.mvc获取checkbox、radio、select的值
    C#面向对象的一些笔记
    Javascript预解析、作用域、作用域链
    解决ajax请求cors跨域问题
    Asp.Net操作WebServices
    2019年科技趋势前10位
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13884097.html
Copyright © 2020-2023  润新知