HDU 4971 a simple brute force problem. (minimum cut, maximum weight closed graph)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4971

A simple brute force problem.Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 182    Accepted Submission(s): 115


Problem descriptionthere's a company with several projects to be done. finish a project will get you profits. however, there are some technical problems for some specific projects. to solve the problem, the Manager will train his employee which may cost his budget. there may be dependencies between technical problems, for example, a requires B means you need to solve problem B before solving proble M. if a requires B and B requires a, it means that you should solve them at the same time. you can select which problems to be solved and how to solve them freely before finish your projects. can you tell me the maximum profit?
 
Inputthe first line of the input is a single integer T (<= 100) which is the number of test cases.

Each test case contains a line with two integer N (<= 20) and M (<= 50) which is the number of project to select to complete and the number of technical problem.

Then a line with N integers. the I-th INTEGER (<= 1000) means the profit of complete the I-th project.

Then a line with M integers. the I-th INTEGER (<= 1000) means the cost of training to solve the I-th technical problem.

Then n lines. Each line contains some integers. The first integer k is the number of technical problems, followed by K integers implying the technical problems need to solve for the I-th project.

After that, there are m lines with each line contains M integers. if the I-th row of the J-th Column is 1, it means that you need to solve the I-th problem before solve the J-th problem. otherwise the I-th row of the J-th Column is 0.
Outputfor each test case, please output a line which is "case # X: Y", X means the number of the test case and Y means the maximum profit.
Sample Input
42 310 106 6 62 0 12 1 20 1 01 0 00 0 02 310 108 10 61 01 20 1 01 0 00 0 02 310 108 10 61 01 20 1 00 0 00 0 02 310 108 10 61 01 20 0 01 0 00 0 0
 
Sample output
Case #1: 2Case #2: 4Case #3: 4Case #4: 6
 
Source2014 multi-university training contest 10

Question:

N projects and M problems are given. to complete a project, some problems need to be solved. To solve a problem, another problem may need to be solved first. For example, problem I depends on problem J, solve J first and then solve I. If they are mutually dependent, they should be resolved at the same time. When a project is completed, the benefits are obtained. It takes some cost to solve a problem and the maximum net income is obtained.

Analysis:

A single question is a network stream, but I never thought about how to build a picture. Later, my teammates cut the question and found that the other teams had a fast journey, I think it should be a messy search (of course, it can be done through a messy search). Later I saw a network stream that I had done and I was very happy to cut it down. After that, I thought about this question again, it was found that it was the largest weighted closed graph. Fortunately, I had a previous record and remembered the method of creating the graph. So I changed the code of the network stream to AC twice.

Graph creation method: the project is a positive weight, the weight is the benefit, the problem is a negative weight, and the weight is the cost. The connection edge of the project to solve the problem. The capacity is INF. The source point is added, and the direct right point is added. The capacity is the benefit. The settlement point is added, and the negative right point is connected to the edge. The capacity is spent. The maximum weight is the sum of the positive and minus the smallest cut.


#include <cstdio>#include <algorithm>#include <cstring>#define LL long long#define itn int#define maxn 1007#define maxm 2333333#define INF 0x3f3f3f3fusing namespace std;int a[maxn],b[maxn];int fir[maxn];itn u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];int e_max;itn q[maxn<<2];itn lv[maxn],iter[maxn];void add_edge(int _u,int _v,int _w){    int e=e_max++;    u[e]=_u;v[e]=_v;cap[e]=_w;    nex[e]=fir[u[e]];fir[u[e]]=e;    e=e_max++;    u[e]=_v;v[e]=_u;cap[e]=0;    nex[e]=fir[u[e]];fir[u[e]]=e;}void dinic_bfs(itn s){    int f,r;    lv[s]=0;    q[f=r=0]=s;    while (f<=r)    {        int x=q[f++];        for (int e=fir[x];~e;e=nex[e])        {            if (cap[e]>flow[e] && lv[v[e]]<0)            {                lv[v[e]]=lv[u[e]]+1;                q[++r]=v[e];            }        }    }}int dinic_dfs(int s,int t,int _f){    if (s==t)   return _f;    for (int &e=iter[s];~e;e=nex[e])    {        if (cap[e]>flow[e] && lv[s]<lv[v[e]])        {            int _d=dinic_dfs(v[e],t,min(cap[e]-flow[e],_f));            if (_d>0)            {                flow[e]+=_d;                flow[e^1]-=_d;                return _d;            }        }    }    return 0;}itn max_flow(int s,int t){    int total_flow=0;    memset(flow,0,sizeof flow);    for (;;)    {        memset(lv,-1,sizeof lv);        dinic_bfs(s);        if (lv[t]==-1)  break;        memcpy(iter,fir,sizeof fir);        itn _f=0;        while ((_f=dinic_dfs(s,t,INF))>0)            total_flow+=_f;    }    return total_flow;}int main(){    int n,m;    itn T_T,cas=0;    scanf("%d",&T_T);    while(T_T--)    {        printf("Case #%d: ",++cas);        scanf("%d%d",&n,&m);        itn s=0,t=n+m+1;        itn sr=0,sc=0;        e_max=0;        memset(fir,-1,sizeof fir);        for (int i=1;i<=n;i++)        {            scanf("%d",a+i);            add_edge(s,i,a[i]);            sr+=a[i];        }        for (int i=1;i<=m;i++)        {            scanf("%d",b+i);            add_edge(i+n,t,b[i]);        }        for (int i=1,k,p;i<=n;i++)        {            scanf("%d",&k);            for (int j=0;j<k;j++)            {                scanf("%d",&p);                p++;                add_edge(i,p+n,INF);            }        }        int x;        for (int i=1;i<=m;i++)        {            for (int j=1;j<=m;j++)            {                scanf("%d",&x);                if (x)                add_edge(i+n,j+n,INF);            }        }        int res=sr-max_flow(s,t);                printf("%d\n",res);    }    return 0;}


HDU 4971 a simple brute force problem. (minimum cut, maximum weight closed graph)

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.