zoukankan      html  css  js  c++  java
  • [gym102471E] Flow(贪心)

    原题

    One of Pang's research interests is the maximum flow problem.

    A directed graph (G) with nn vertices is universe if the following condition is satisfied:

    • (G) is the union of (k) vertex-independent simple paths from vertex (1) to vertex (n) of the same length.

    A set of paths is vertex-independent if they do not have any internal vertex in common.

    A vertex in a path is called internal if it is not an endpoint of that path.

    A path is simple if its vertices are distinct.

    Let (G) be a universe graph with nn vertices and mm edges. Each edge has a non-negative integral capacity. You are allowed to perform the following operation any (including (0)) times to make the maximum flow from vertex (1) to vertex nn as large as possible:

    Let ee be an edge with positive capacity. Reduce the capacity of ee by (1) and increase the capacity of another edge by (1).

    Pang wants to know what is the minimum number of operations to achieve it?

    Input

    The first line contains two integers nn and mm ((2≤n≤100000,1≤m≤200000)).

    Each of the next mm lines contains three integers x,yx,y and zz, denoting an edge from (x) to (y) with capacity (z (1≤x,y≤n, 0≤z≤1000000000)).

    It's guaranteed that the input is a universeuniverse graph without multiple edges and self-loops.

    Output

    Output a single integer — the minimum number of operations.

    Examples

    input

    4 3
    1 2 1
    2 3 2
    3 4 3
    

    output

    1
    

    input

    4 4
    1 2 1
    1 3 1
    2 4 2
    3 4 2
    

    output

    1
    

    思路

    要使得总流量最大,深度相同的边容量相加最接近tot/len,并且当前边的容量不少于同一条路径的上一条边的容量,对每条路径的容量排序,每次向当前最少容量的边填充,这样可以使得操作次数最少。

    [[读题要仔细orz

    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <list>
    #include <map>
    #include <iostream>
    #include <iomanip>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <unordered_map>
    #include <vector>
    #define LL long long
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f3f3f
    #define PI 3.1415926535898
    #define F first
    #define S second
    #define endl '
    '
    #define lson rt << 1
    #define rson rt << 1 | 1
    #define lowbit(x) (x & (-x))
    #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    #define _per(i, a, b) for (int i = (a); i >= (b); --i)
    using namespace std;
    const int maxn = 1e5 + 7;
    int n, m, cnt;
    LL ans = 0;
    vector<pair<int, LL>> G[maxn];
    vector<LL> val[maxn];
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n >> m;
        int x, y;
        LL w;
        LL tot = 0;
        _rep(i, 1, m)
        {
            cin >> x >> y >> w;
            G[x].push_back({y, w});
            tot += w;
        }
        f(i, 0, G[1].size())
        {
            ++cnt;
            int tmp = G[1][i].F;
            val[cnt].push_back(G[1][i].S);
            while (tmp != n)
            {
                val[cnt].push_back(G[tmp][0].S);
                tmp = G[tmp][0].F;
            }
        }
        _rep(i, 1, cnt) sort(val[i].begin(), val[i].end());
        LL k = m / cnt;
        LL ave = tot / k;
        f(i, 0, k)
        {
            LL sum = 0;
            _rep(j, 1, cnt) sum += val[j][i];
            ans += max(0LL, ave - sum);
        }
        cout << ans << endl;
    }
    
  • 相关阅读:
    斯坦福大学Andrew Ng教授主讲的《机器学习》公开课观后感
    关于内推,你该知道的点点滴滴
    向大学说拜拜——大学 > 兴趣 + 时间 + 思考 + 实践
    源码浅析:InnoDB聚集索引如何定位到数据的物理位置,并从磁盘读取
    5.7.17版本mysqlbinlog实时拉取的二进制日志不完整的原因分析
    InnoDB的ibd数据文件为什么比data_length+index_length+data_free的总和还要大?
    gh-ost工具在线改表过程的详细解析
    MySQL5.7 使用utf8mb4字符集比latin1字符集性能低25%,你敢信?
    通过slow query log可以查出长时间未提交的事务吗?用实验+源码来揭晓答案
    源码浅析:MySQL一条insert操作,会写哪些文件?包括UNDO相关的文件吗?
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/14113932.html
Copyright © 2011-2022 走看看