2018 ACM-ICPC Chinese College Students Programming Contest Online Competition F. Clever King (Maximum Weight Closed Subgraph)

Description:

    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].

Ignore extra spaces at the end of each line of output

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

topic source

2018 ACM-ICPC Chinese College Students Programming Competition Online Competition




Problem-solving idea: The naked maximum weight closed subgraph problem, but this problem also has dependencies between items, so the items should also be connected. Maximum weight closed subgraph = minimum cut after reasonable mapping = maximum flow. Recommended blog


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 205005;
const int INF = 0x3f3f3f3f;

int N, M;
int P[MAXN];

int A[MAXN];
int B[MAXN];
int C[MAXN];

int cnt = 1;
int S, T;
struct Edge
{
    int v, next;
    ll flow;
} e[MAXN << 1];

int head[MAXN], edge_num, layer[MAXN];

void addedge(int u, int v, ll w)
{
    e [edge_num] .v = v;
    e[edge_num].flow = w;
    e[edge_num].next = head[u];
    head[u] = edge_num++;

    e [edge_num] .v = u;
    e[edge_num].flow = 0;
    e[edge_num].next = head[v];
    head[v] = edge_num++;
}

bool bfs(int start, int End)
{
    queue<int> Q;
    Q.push(start);
    memset(layer, 0, sizeof(layer));
    layer[start] = 1;
    while (Q.size())
    {
        int u = Q.front();
        Q.pop();

        if (u == End)
            return true;

        for (int j = head[u]; j != -1; j = e[j].next)
        {
            int v = e[j].v;

            if (layer[v] == false && e[j].flow)
            {
                layer[v] = layer[u] + 1;
                Q.push(v);
            }
        }
    }

    return false;
}
ll dfs(int u, ll MaxFlow, int End)
{
    if (u == End)
        return MaxFlow;

    ll uflow = 0;

    for (int j = head[u]; j != -1; j = e[j].next)
    {
        int v = e[j].v;

        if (layer[v] - 1 == layer[u] && e[j].flow)
        {
            ll flow = min(MaxFlow - uflow, e[j].flow);
            flow = dfs(v, flow, End);

            e[j].flow -= flow;
            e[j ^ 1].flow += flow;

            uflow += flow;

            if (uflow == MaxFlow)
                break;
        }
    }
    if (uflow == 0)
        layer[u] = 0;
    return uflow;
}
ll dinic(int start, int End)
{
    ll MaxFlow = 0;

    while (bfs(start, End))
        MaxFlow += dfs(start, INF, End);
    return MaxFlow;
}

int SP[MAXN];
int TP[MAXN];
vector<int> ore[MAXN];
vector<int> pro[MAXN];

intmain()
{
    int T1;
    scanf("%d", &T1);
    while (T1--)
    {

        memset(head, -1, sizeof(head));
        edge_num = 0;
        scanf("%d%d", &N, &M);

        ll sum = 0;
        for (int i = 1; i <= N; i++){
            scanf("%d", &P[i]);
            sum+=P[i];
        }

        for (int i = 1; i <= M; i++)
        {
            scanf("%d", &C[i]);
        }

        int temp;
        int num1,num2;
        for(int i=1;i<=N;i++){
            ore[i].clear();
            pro[i].clear();
            scanf("%d%d",&num1,&num2);
            for(int j=0;j<num1;j++){
                scanf("%d",&temp);
                ore[i].push_back(temp);
            }
            for(int j=0;j<num2;j++){
                scanf("%d",&temp);
                pro[i].push_back(temp);
            }
        }



        cnt = 0;
        S = ++cnt;
        for (int i = 1; i <= N; i++)
            SP[i] = ++cnt;
        for (int i = 1; i <= M; i++)
            TP[i] = ++cnt;

        T = ++cnt;

        for (int i = 1; i <= N; i++)
            addedge(S, SP[i], P[i]);

        for (int i = 1; i <= N; i++)
        {
            for(int j=0;j<ore[i].size();j++){
                addedge(SP[i], TP[ore[i][j]], INF);
            }
            for(int j=0;j<pro[i].size();j++){
                addedge(SP[i], SP[pro[i][j]], INF);
            }
        }

        for (int i = 1; i <= M; i++)
            addedge(TP[i], T, C[i]);

        printf("%lld\n", sum - dinic(S, T));
    }
    return 0;
}

Guess you like

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