hdu 多校联合 4971 A simple brute force problem.(最大权闭合图)

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个问题,完成某个项目需要解决一些问题,解决某个问题可能要先解决另一个问题,比如问题i依赖于问题j,那要先解决j再解决i,如果互相依赖,则要同时解决。完成某个项目会获得收益,解决某个问题需要一些花费,求最大净收益。
分析: (项目)n个正权值点,(问题)m个负权值点,且有依赖关系,i问题依赖j问题;求最大收益,符合最大权闭合图(当时没想到) , 源点s连n个正权值点,流量为收益,m个负权值点连汇点t ,流量为花费的钱;每个项目与 需要攻克的问题连边,流量为INF,每个问题之间的依赖连边,流量为INF,求最小割,即求一遍最大流,最大收益为 = 正权值之和-最大流 ;(对最大权闭合图没印象的翻之前的来看);
#include<cstdio>  
#include<cstring>  
#include<map>  
#include<vector>  
#include<cmath>  
#include<cstdlib>  
#include<stack>  
#include<queue>  
#include <iomanip>  
#include<iostream>  
#include<algorithm>  
using namespace std ;  
const int N=100 ;  
const int M=50000 ;  
const int inf=1<<30 ;  
struct node  
{  
    int u,v,c,next;  
}edge[M];  
int pre[N],cur[N],gap[N],dis[N],head[N];  
int top ;  
  
  
int add(int u ,int v,int c)  
{  
    edge[top].u=u;  
    edge[top].v=v;  
    edge[top].c=c;  
    edge[top].next=head[u];  
    head[u]=top++;  
    edge[top].u=v;  
    edge[top].v=u;  
    edge[top].c=0;  
    edge[top].next=head[v];  
    head[v]=top++;  
}  
  
int sap(int s,int t,int nv)  
{  
    memset(dis,0,sizeof(dis));  
    memset(gap,0,sizeof(gap));  
    for(int i = 0 ; i < nv ; i++)   cur[i]=head[i];  
    int minflow=inf ,flow = 0 ,u,v;  
    pre[s]=u=s;  
    gap[s]=nv;  
    while(dis[s]<nv)  
    {  
            loop :  
                 for(int &j = cur[u]; j!=-1;j=edge[j].next)  
                 {  
                       v=edge[j].v ;  
                       if(edge[j].c > 0 && dis[u] == dis[v]+1)  
                       {  
                               pre[v]=u;  
                               u=v;  
                               minflow = min(minflow,edge[j].c);  
                               if(v==t)  
                               {  
                                    for(u=pre[v];v!=s;v=u,u=pre[u])  
                                    {  
                                        edge[cur[u]].c -= minflow ;  
                                        edge[cur[u]^1].c += minflow ;  
                                    }  
                                    flow += minflow;  
                                    minflow = inf ;  
                               }  
                             goto loop ;  
                       }  
                 }  
               int mindis=nv ;  
               for(int i = head[u] ; i!=-1 ; i=edge[i].next)  
               {  
                    v=edge[i].v;  
                    if(edge[i].c>0 && dis[v] < mindis)  
                    {  
                             cur[u]=i;  
                             mindis=dis[v];  
                    }  
               }  
               if(--gap[ dis[u] ]==0) break;  
               gap[ dis[u]=mindis+1 ]++;  
               u=pre[u];  
    }    
     return flow;  
}  

int main()
{
      int  n , m , T,x,k,q,tt=0;
      scanf("%d",&T);
	  while(T--)
	  {
	  	    scanf("%d%d",&n,&m);
	  	    int s=0,t=n+m+1 ,nv=t+1;
			top=0;
	  	    memset(head,-1,sizeof(head));
	  	    int num = 0 ;
	  	    for(int i = 1 ; i <= n ; i++) //汇点连正权值点 
	  	    {
	  	    	  scanf("%d",&x);
	  	    	  add(s,i,x);
	  	    	  num += x ;
	  	    }
	  	    for(int i = 1 ; i <= m ; i++)//负权值点连汇点 
	  	    {
	  	    	  scanf("%d",&x);
	  	    	  add(i+n,t,x) ;
	  	    }
	  	  for(int i = 1 ; i <= n ; i++)
	  	  {
	  	  	  scanf("%d",&k);
	  	  	  for(int  j = 0 ; j < k ; j++)
	  	  	  {
	  	  	  	  scanf("%d",&q);
	  	  	  	  q++;
	  	  	  	  add(i,n+q,inf); //项目连问题 
	  	  	  }
	  	  	
	  	  }  //printf("**********");
	  	  for(int i = 1 ; i<= m ; i++ )
			 for(int j = 1 ; j <= m ; j++)
			   {
			   	     scanf("%d",&x);
			   	     if(x)
			   	     add(i+n,j+n,inf);	   	//问题之间连边 
			   } 
			int ans = num - sap(s,t,nv) ;   
	  	  printf("Case #%d: %d\n",++tt,ans);
	  } 
      return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值