hdu4971(最大流,最大权闭合子图)

202 篇文章 0 订阅
123 篇文章 0 订阅

A simple brute force problem.

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 618    Accepted Submission(s): 323


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个技术难关,每个技术难关都要通过培训员工来解决,并且要花费一定金钱。每个工程都需要克服几个技术难关才能完成,技术难关数量也可以为0. 有的技术难关需要在其他技术难关克服的前提下才能克服。 问获得的最大利润为多少?


闭合图:一个有向图的子点集,使其中的点的出边都指回集合中的点,则称此为闭合图。

最大权闭合图:给每个点赋上点权,则权和最大的闭合图,为最大权闭合图。

算法(求最大权闭合子图):

(1)新增源点和汇点s,t

(2)对于原图中的边u->v,增加一条u->v的容量为无穷大的边

(3)对于点u,如果w[u]>0,增加一条s->u的容量为w[u]的边;否则增加一条u->v的容量为-w[u]的边


定义一个割划分出的S集合为一个解,那么割集的容量之和就是(未被选的A集合中的顶点的权值 + 被选的B集合中的顶点的权值),记为Cut。A集合中所有顶点的权值之和记为Total,那么Total - Cut就是(被选的A集合中的顶点的权值 - 被选的B集合中的顶点的权值),即为我们的目标函数,记为A。要想最大化目标函数A,就要尽可能使Cut小,Total是固定值,所以目标函数A取得最大值的时候,Cut最小,即为最小割。


用途:闭合图的性质恰好反映了事件之间的必要条件的关系:一个事件发生,它需要的所有前提都要发生。


这题我的边建反了,不过原理是一样的。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iomanip>
using namespace std;
typedef long long ll;
const   int oo=1e9;
/**oo 表示无穷大*/
const  int mm=111111111;
/**mm 表示边的最大数量,记住要是原图的两倍,在加边的时候都是双向的*/
const  int mn=2010;
/**mn 表示点的最大数量*/
int node,src,dest,edge;
/**node 表示节点数,src 表示源点,dest 表示汇点,edge 统计边数*/
int ver[mm],flow[mm],nex[mm];
int head[mn],work[mn],dis[mn],q[mn];
void prepare(int _node, int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<=node; ++i)head[i]=-1;
    edge=0;
}
void addedge( int u,  int v,  int c)
{
    ver[edge]=v,flow[edge]=c,nex[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,nex[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=nex[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)  return 1;
            }
    return 0;
}
int Dinic_dfs(  int u, int exp)
{
    if(u==dest)  return exp;
    for(  int &i=work[u],v,tmp; i>=0; i=nex[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while((delta=Dinic_dfs(src,oo)))ret+=delta;
    }
    return ret;
}

int main()
{
    int t,o=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int st=0,ed=n+m+1,sum=0;
        prepare(n+m+3,st,ed);
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            addedge(i+m,ed,x);
            sum+=x;
        }
        for(int i=1; i<=m; i++)
        {
            int x;
            scanf("%d",&x);
            addedge(st,i,x);
        }
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            while(x--)
            {
                int xx;
                scanf("%d",&xx);
                addedge(1+xx,i+m,oo);
            }
        }
        for(int i=1; i<=m; i++)
            for(int j=1; j<=m; j++)
            {
                int x;
                scanf("%d",&x);
                if(x==1)
                    addedge(j,i,oo);
            }
        printf("Case #%d: %d\n",o++,sum-Dinic_flow());
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值