[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;
}
原文地址:https://www.cnblogs.com/hfcdyp/p/14113932.html