• 【luogu P1396】营救


    https://www.luogu.org/problem/show?pid=1396

    弱化版的货车运输,用并查集维护连通块,将边按权值升序排序后依次插入直到两点连通,最后插入的边的权值就是最小的拥挤度最大值。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #define maxn 100005
    using namespace std;
    int n, m, s, t;
    struct edge
    {
        int from, to, weight;
    };
    bool operator<(const edge &x, const edge &y)
    {
        return x.weight < y.weight;
    }
    vector<edge> e;
    namespace djs
    {
    int parent[maxn];
    void init()
    {
        for (int i = 1; i <= n; i++)
            parent[i] = -1;
    }
    int find(int x)
    {
        if (parent[x] < 0)
            return x;
        else
            return parent[x] = find(parent[x]);
    }
    void merge(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x == y)
            return;
        if (parent[x] > parent[y])
            swap(x, y);
        parent[x] += parent[y];
        parent[y] = x;
    }
    bool related(int x, int y)
    {
        return find(x) == find(y);
    }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin >> n >> m >> s >> t;
        int a, b, c;
        for (int i = 0; i < m; i++)
        {
            cin >> a >> b >> c;
            e.push_back((edge){a, b, c});
        }
        sort(e.begin(), e.end());
        djs::init();
        int ans = 0;
        for (int i = 0; i < e.size() && !djs::related(s, t); i++)
        {
            djs::merge(e[i].from, e[i].to);
            ans = e[i].weight;
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    基于JFinal中搭建wopi协议支撑办法
    mysql 增加列
    国王分金币
    口算题卡升级版本
    elasticsearch牛人的日志列表
    牛B的大数据库
    golang --rune
    golang ---rune与byte
    golang学习笔记--接口
    golang学习笔记--函数和方法
  • 原文地址:https://www.cnblogs.com/ssttkkl/p/7531939.html
Copyright © 2020-2023  润新知