Network Flow Problem - Maximum Weighted Closed Path 2018 ACM-ICPC Chinese College Students Programming Contest Online Competition F. Clever King

Example 2018 ACM-ICPC Chinese College Students Programming Contest Online Competition F. Clever King

In order to increase the happiness index of people’s lives, King Y has decided to develop the manufacturing industry vigorously. There are total n kinds of products that King can choose to produce, different products can improve the happiness index of poeple’s lives in different degrees, of course, the production of goods needs raw materials, different products need different ore or other products as raw materials. There are total m mines, and each mine can exploit different ore, Therefore, there are m types of ores, the cost of each mining for each mine is different, king Y want to maximize the income, the calculation method of income is:∑increased happiness index - ∑mining costs.
If you choose to exploit a mine, there will be an unlimited number of this kind of ore. What’s more, if you produce one product, the happiness index will definitely increase, no matter how many you produce.

Input

The first line of the input has an integer T(1<=T<=50), which represents the number of test cases.
In each test case, the first line of the input contains two integers n(1<=n<=200)–the number of the products and m(1<=m<=200)–the number of mines. The second line contains n integers, val[i] indicates the happiness index that number i product can increase. The third line contains m integers, cost[i] indicates the mining cost of number i mine. The next n lines, each line describes the type of raw material needed for the number i product, in each line, the first two integers n1(1<=n1<=m)–the number of ores that this product needs, n2(1<=n2<=n)–the number of products that this product needs, the next n1 + n2 integers indicate the id of ore and product that this product needs. it guarantees that ∑n1+∑n2<=2000.

Output

Each test case output an integer that indicates the maximum value ∑val[i]-∑cost[i].

Sample Input

2
3 3
600 200 400
100 200 300
1 2 1 2 3
1 0 2
1 0 3
3 4
600 400 200
100 200 300 1000
2 1 1 2 3
1 0 1
1 0 1

Sample Output

600
900

Cut problem (source: Hu Botao, "Application of Minimum Cut Model in Informatics Competition")

write picture description here
write picture description here
write picture description here
write picture description here
write picture description here
write picture description here

Maximum weighted closed path problem (source: Hu Botao, "Application of Minimum Cut Model in Informatics Competition")

write picture description here
write picture description here
write picture description here
write picture description here
write picture description here
To sum up: the maximum weighted closed path is equal to the sum of all positive weights minus the minimum cut (ie, the maximum flow)

Analysis of this topic

In this way, the problem is a bare maximum weighted closed path. Create a new starting node to connect to all products (positive value brought by the production of the product), and the capacity is the weight.
Then create a new end node to connect to all minerals (the consumption of mining minerals), and the capacity is the weight
. Dependency of production The relationship is transformed into the connection of edges, and the capacity is positive infinity.
According to the above definition, the answer to this question is:
the positive value brought by the production of the product - the minimum cut (ie the maximum flow)

Sample implementation code

/*
ZhangBinjie@Penguin
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define maxn 1000+2
#define inf 0x7f7f7f7f
using namespace std;

struct Edge {
    int from, to;
    long long cap, flow;
    Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
};

struct EDmondsKarp {
    int n, m;
    vector<Edge>edges;
    vector<int>G[maxn];
    long long a[maxn];
    int p[maxn];

    void init() {
        for (int i = 0; i<maxn; ++i)
            G[i].clear();
        edges.clear();
    }

    void AddEdge(int from, int to, long long cap) {
        edges.push_back(Edge(from, to, cap, 0));
        edges.push_back(Edge(to, from, 0, 0));
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }

    long long Maxflow(int s, int t) {
        long long flow = 0;
        for (;;) {
            memset(a, 0, sizeof(a));
            queue<int>Q;
            Q.push(s);
            a[s] = inf;
            while (!Q.empty()) {
                int x = Q.front();
                Q.pop();
                for (int i = 0; i<G[x].size(); i++) {
                    Edge& e = edges[G[x][i]];
                    if (!a[e.to] && e.cap>e.flow) {
                        p[e.to] = G[x][i];
                        a[e.to] = min(a[x], e.cap - e.flow);
                        Q.push(e.to);
                    }
                }
                if (a[t])
                    break;
            }
            if (!a[t])
                break;
            for (int u = t; u != s; u = edges[p[u]].from) {
                edges[p[u]].flow += a[t];
                edges[p[u] ^ 1].flow -= a[t];
            }
            flow += a[t];
        }
        return flow;
    }
};

int main() {
    int T;
    int n, m, a, b, t;
    long long sum;
    long long mincut;
    EDmondsKarp e;
    cin >> T;
    while (T--) {
        sum = 0;
        e.init();
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) {
            cin >> t;
            e.AddEdge(0, i, t);
            sum += t;
        }
        for (int i = 1; i <= m; ++i) {
            cin >> t;
            e.AddEdge(i + 500, 1000, t);
        }
        for (int i = 1; i <= n; ++i) {
            cin >> a >> b;
            for (int j = 0; j<a; ++j) {
                cin >> t;
                e.AddEdge(i, t + 500, inf);
            }
            for (int j = 0; j<b; ++j) {
                cin >> t;
                e.AddEdge(i, t, inf);
            }
        }
        mincut = sum - e.Maxflow(0, 1000);
        cout << mincut << endl;
    }

    return 0;
}

result

write picture description here

uninstall last

The source of the template is the template in Liu Rujia's book.
Note that the relevant places are changed to long long
. After testing, the maximum flow template of Zhejiang University will time out. The template of
Jilin University is to be tested.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324918278&siteId=291194637