HDU 4971 A simple brute force problem. 最小割 最大权闭合

题目描述:

Description
There’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 problem A. 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?

Input
The 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.

Output
For each test case, please output a line which is “Case #X: Y “, X means the number of the test case and Y means the the maximum profit.

Sample Input

4
2 3
10 10
6 6 6
2 0 1
2 1 2
0 1 0
1 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 1 0
1 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 1 0
0 0 0
0 0 0
2 3
10 10
8 10 6
1 0
1 2
0 0 0
1 0 0
0 0 0 

Sample Output

Case #1: 2
Case #2: 4
Case #3: 4
Case #4: 6 

题目分析:

n个项目,m个技术问题,其中每个项目有一定的价值(profits),攻关技术问题也需要一定的花费(cost)。每个项目又有需要攻克的技术问题,技术问题之间的攻克顺序也有一定顺序(也可能是需要同时完成)。
求获得最大利润(总价值-总花费)。
这题其实是个最大权闭合的模板题,我们可以将其转化成最小割问题。
http://www.cnblogs.com/wuyiqi/archive/2012/03/12/2391960.html
我就是通过这个博客学习的。
设源点s=0,汇点t=n+m+1。对n个项目,与s建边,权值为其价值。m个技术问题,与t建边,权值为其花费。项目与问题,问题与问题的先后关系之间建边,权值为INF。跑一遍最小割(最大流)。总价值减这个最小割就可以得出答案。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MAXN=1010;

struct node
{
    int v;
    int w;
    int next;
}mp[MAXN<<5];
int id;
int q[MAXN];
int level[MAXN];
int head[MAXN];

void init()
{
    memset(head,-1,sizeof(head));
    id=0;
}

void addedge(int u,int v,int w)
{
    mp[id].v=v;
    mp[id].w=w;
    mp[id].next=head[u];
    head[u]=id++;

    mp[id].v=u;
    mp[id].w=0;
    mp[id].next=head[v];
    head[v]=id++;
}

int bfs(int s, int t) //构建层次网络
{
    memset(level,0,sizeof(level));
    level[s]=1;
    int front=0,rear=1;
    q[front]=s;
    while(front < rear)
    {
        int x=q[front++];
        if(x==t) return 1;
        for(int e=head[x]; e!=-1; e=mp[e].next)
        {
            int v=mp[e].v, f=mp[e].w;
            if(!level[v] && f)
            {
                level[v]=level[x]+1;
                q[rear++]=v;
            }
        }
    }
    return 0;
}

int dfs(int u,int maxf,int t)
{
    if(u==t) return maxf;
    int ret=0;
    for(int e=head[u]; e!=-1; e=mp[e].next)
    {
        int v=mp[e].v, f=mp[e].w;
        if(level[u]+1==level[v] && f)
        {
            int Min=min(maxf-ret,f);
            f=dfs(v,Min,t);
            mp[e].w-=f;
            mp[e^1].w+=f;
            ret+=f;
            if(ret==maxf) return ret;
        }
    }
    return ret;
}

int dinic(int s, int t) //s源点 t汇点
{
    int ans = 0;
    while(bfs(s,t))
       ans+=dfs(s,INF,t);
    return ans;
}

int n,m;

int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        init();
        int sum=0;
        int pro[MAXN];
        int cost[MAXN];
        scanf("%d%d",&n,&m);
        int s=0,t=n+m+1;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&pro[i]);
            addedge(s,i,pro[i]);
            sum+=pro[i];
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&cost[i]);
            addedge(i+n,t,cost[i]);
        }
        for(int i=1; i<=n; i++)
        {
            int k;
            scanf("%d",&k);
            for(int j=0; j<k; j++)
            {
                int tt;
                scanf("%d",&tt);
                tt++;
                addedge(i,tt+n,INF);
            }
        }
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=m; j++)
            {
                int x;
                scanf("%d",&x);
                if (x) addedge(i+n,j+n,INF);
            }
        }
        int ans=sum-dinic(s,t);
        printf("Case #%d: %d\n",cas,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值