2019杭电多校第二场 H Harmonious Army 网络流最小割

问题 H: Harmonious Army

时间限制: 1 Sec  内存限制: 128 MB
提交: 63  解决: 20
[提交] [状态] [命题人:admin]

题目描述

Now, Bob is playing an interesting game in which he is a general of a harmonious army. There are n soldiers in this army. Each soldier should be in one of the two occupations, Mage or Warrior. There are m pairs of soldiers having combination ability. There are three kinds of combination ability. If the two soldiers in a pair are both Warriors, the army power would be increased by a. If the two soldiers in a pair are both Mages, the army power would be increased by c. Otherwise the army power would be increased by b, and b=a/4+c/3, guaranteed that 4|a and 3|c. Your task is to output the maximum power Bob can increase by arranging the soldiers' occupations.

Note that the symbol a|b means that a divides b, e.g. , 3|12 and 8|24.

 

输入

There are multiple test cases.
Each case starts with a line containing two positive integers n(n≤500) and m(m≤104).
In the following m lines, each line contains five positive integers u,v,a,b,c (1≤u,v≤n,u≠v,1≤a,c≤4×106,b=a/4+c/3), denoting soldiers u and v have combination ability, guaranteed that the pair (u,v) would not appear more than once.
It is guaranteed that the sum of n in all test cases is no larger than 5×103, and the sum of m in all test cases is no larger than 5×104.

 

输出

For each test case, output one line containing the maximum power Bob can increase by arranging the soldiers' occupations.

 

样例输入

复制样例数据

3 2
1 2 8 3 3
2 3 4 3 6

样例输出

12

 

[提交][状态]

题意:对n个点黑白染色,若都为黑,贡献加a,若都为白,贡献加c,否则加b=a/4+c/3

据说网络流做多了就知道要建图求最小割了

(附官方题解的图)

本题要使权值最大,最小割求最小,那么就求出所有权值之和,通过构图使得减去最小割就是答案

若都为黑,假设都划分到s集合下,那么删去c,d两边,也就是A/4+4*C/3,那么c,d赋值为该值的二倍

若都为白,划分到t集合下,那么删去a,b两边,也就是5*A/4+C/3,a,b也赋值为二倍

若一黑一白,则删去a,e,d或b,e,c,也就是A+C,那e赋值为3*A/4+C/6

代码:

#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 2e5 + 100;
const int inf = 0x3f3f3f3f;


struct Dinic {
    struct Edge {
        int next, to;
        ll f;
    } e[maxn];
    int head[maxn];
    ll dep[maxn], tol;
    ll ans;
    int cur[maxn];
    int src, sink, n;

    void add(int u, int v, int f) {
        tol++;
        e[tol].to = v;
        e[tol].next = head[u];
        e[tol].f = f;
        head[u] = tol;
        tol++;
        e[tol].to = u;
        e[tol].next = head[v];
        e[tol].f = 0;
        head[v] = tol;
    }

    bool bfs() {
        queue<int> q;
        for (int i = 0; i <= n; ++i) dep[i] = -1;
        q.push(src);
        dep[src] = 0;
        while (!q.empty()) {
            int now = q.front();
            q.pop();
            for (int i = head[now]; i; i = e[i].next) {
                if (dep[e[i].to] == -1 && e[i].f) {
                    dep[e[i].to] = dep[now] + 1;
                    if (e[i].to == sink)
                        return true;
                    q.push(e[i].to);
                }
            }
        }
        return false;
    }

    ll dfs(int x, ll maxx) {
        if (x == sink)
            return maxx;
        for (int &i = cur[x]; i; i = e[i].next) {
            if (dep[e[i].to] == dep[x] + 1 && e[i].f > 0) {
                ll flow = dfs(e[i].to, min(maxx, e[i].f));
                if (flow) {
                    e[i].f -= flow;
                    e[i ^ 1].f += flow;
                    return flow;
                }
            }
        }
        return 0;
    }

    ll dinic(int s, int t) {
        ans = 0;
        this->src = s;
        this->sink = t;
        while (bfs()) {
            for (int i = 0; i <= n; ++i)
                cur[i] = head[i];
            while (ll d = dfs(src, inf))
                ans += d;
        }
        return ans;
    }

    void init(int n) {
        this->n = n;
        for (int i = 0; i <= n; ++i) head[i] = 0;
        tol = 1;
    }
} G;

int main() {
    int n, m;
    while (~scanf("%d%d", &n, &m)) {
        G.init(n + 2);
        int u, v, a, b, c;
        ll ans = 0;
        for (int i = 1; i <= m; i++) {
            scanf("%d%d%d%d%d", &u, &v, &a, &b, &c);
            ans += (a + b + c) * 2;  //扩大二倍,防止除不尽出现小数
            G.add(n + 1, u, 5 * a / 4 + c / 3);
            G.add(n + 1, v, 5 * a / 4 + c / 3);
            G.add(u, n + 2, a / 4 + 4 * c / 3);
            G.add(v, n + 2, a / 4 + 4 * c / 3);
            G.add(u, v, a / 2 + c / 3);
            G.add(v, u, a / 2 + c / 3);
        }
        ans -= G.dinic(n + 1, n + 2);
        printf("%lld\n", ans / 2);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值