HDU-4971-a simple brute force problem. (Max weight closure diagram)

Source: Internet
Author: User

Question: n (n <= 20) projects, M (M <= 50) technical issues, and profit (<= 1000) after a project is completed ), after a project is completed, the corresponding technical problems must be solved, and cost (<= 1000) is required to solve a technical problem. The technical problems are dependent on each other to maximize the benefits.

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4971

--> The project must solve the corresponding technical problems, and there are dependencies between the technical problems, corresponding to the closed diagram, and the maximum benefit corresponds to the maximum right .. As a result, the largest right closed chart, the smallest cut, and the largest stream playing ..

Graph creation:

1) Super source S = N + M, super sink t = n + m + 1

2) for each project I: S-> I (profit [I])

3) for each technical question I: I + N-> T (cost [I])

4) technical issues that must be addressed by project I j: I-> J + N (INF)

5) the technical problem J must solve first I: I + N-> J + N (INF) (here I think it should be: J + N-> I + N (INF). This understanding is correct. However, if you are not familiar with the sample, you can submit wa ..)

Then, dinic goes on stage ..

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using std::queue;using std::min;const int MAXN = 20 + 50 + 10;const int MAXM = 20 + 1000 + 2500 + 50 + 10;const int INF = 0x3f3f3f3f;int n, m, sum;int hed[MAXN];int cur[MAXN], h[MAXN];int ecnt;int S, T;struct EDGE{    int to;    int cap;    int flow;    int nxt;} edges[MAXM << 1];void Init(){    ecnt = 0;    memset(hed, -1, sizeof(hed));    sum = 0;}void AddEdge(int u, int v, int cap){    edges[ecnt].to = v;    edges[ecnt].cap = cap;    edges[ecnt].flow = 0;    edges[ecnt].nxt = hed[u];    hed[u] = ecnt++;    edges[ecnt].to = u;    edges[ecnt].cap = 0;    edges[ecnt].flow = 0;    edges[ecnt].nxt = hed[v];    hed[v] = ecnt++;}void Read(){    int profit, cost, pc, tp;    scanf("%d%d", &n, &m);    S = n + m;    T = n + m + 3;    for (int i = 0; i < n; ++i)    {        scanf("%d", &profit);        AddEdge(S, i, profit);        sum += profit;    }    for (int i = 0; i < m; ++i)    {        scanf("%d", &cost);        AddEdge(i + n, T, cost);    }    for (int i = 0; i < n; ++i)    {        scanf("%d", &pc);        for (int j = 0; j < pc; ++j)        {            scanf("%d", &tp);            AddEdge(i, tp + n, INF);        }    }    for (int i = 0; i < m; ++i)    {        for (int j = 0; j < m; ++j)        {            scanf("%d", &tp);            if (tp)            {                AddEdge(i + n, j + n, INF);            }        }    }}bool Bfs(){    memset(h, -1, sizeof(h));    queue<int> qu;    qu.push(S);    h[S] = 0;    while (!qu.empty())    {        int u = qu.front();        qu.pop();        for (int e = hed[u]; e != -1; e = edges[e].nxt)        {            int v = edges[e].to;            if (h[v] == -1 && edges[e].cap > edges[e].flow)            {                h[v] = h[u] + 1;                qu.push(v);            }        }    }    return h[T] != -1;}int Dfs(int u, int cap){    if (u == T || cap == 0) return cap;    int flow = 0, subFlow;    for (int e = cur[u]; e != -1; e = edges[e].nxt)    {        cur[u] = e;        int v = edges[e].to;        if (h[v] == h[u] + 1 && (subFlow = Dfs(v, min(cap, edges[e].cap - edges[e].flow))) > 0)        {            flow += subFlow;            edges[e].flow += subFlow;            edges[e ^ 1].flow -= subFlow;            cap -= subFlow;            if (cap == 0) break;        }    }    return flow;}int Dinic(){    int maxFlow = 0;    while (Bfs())    {        memcpy(cur, hed, sizeof(hed));        maxFlow += Dfs(S, INF);    }    return maxFlow;}int main(){    int t, kase = 0;    scanf("%d", &t);    while (t--)    {        Init();        Read();        printf("Case #%d: %d\n", ++kase, sum - Dinic());    }    return 0;}


HDU-4971-a simple brute force problem. (Max weight closure diagram)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.