[HOJ2634] How to earn more 最大权闭合子图

Xiao Ming is an expert in computer science and technology, so he can get a lot of projects every month. The projects always bring him a lot of money, now he is thinking how to earn money as more as possible.

Every month he can get m projects, and each project Ai will bring him Xi yuan. Although Xiao Ming is an expert, he still needs to hire some other guys to help him. Of course, the employees are not as good as Xiao Ming, for they are just good at some single aspect. So, they should work together to finish one project. There is a list shows the salary of m employees, who are labeled from 0 to m-1. Xiao Ming only hires employees, in that list, and he knows who will be needed by each project.If one employee is hired, he can join in several projects.

Input

The first line is an integer c shows the number of cases. For each case, the first line has two numbers m,n(m,n <=100), denoting that there is m projects and n employees on the list.The second line has m integers, which are seperated by a single blank, the ith number Ximeans the project Ai will bring Xiao Ming Xi yuan. Xi is less the 10000. The third line has n integers, which are seperated by a single blank, the ith number Yimeans the employee Bi will cost Xiao Ming Yi yuan. And the next m lines will show which part of the employees will be needed by each project. Line i is a list of the employees, who are needed by project Ai. In each line, first a number Zi shows the number of employees needed by this project. And Zi labels of the emloyees follows, which are still seperated by a sigle blank.

Output

You should output a single integer shows the maximun money Xiao Ming can earn in a single month. The money he can earn is equall to the money he can totally get minus the money he totally cost. You should not leave any extra blanks at the end of each line.

Sample Input

1
3 5
30 40 43
55 17 23 22 11
3 0 1 2
3 1 2 3
2 2 1

Sample Output

21

Hint

If Xiao Ming can do less project to earn more money, he will certainly do that.

题解:

最大权闭合子图水题,每一个工作任务有一条边 容量为输入的利润,每一个员工到汇点有一条边 容量为雇佣费,

每一个工作任务到需要的员工有一条边,容量为INF.

答案就是利润和-最小割

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
const int N=,INF=;
int gi(){
int str=;char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
return str;
}
int n,m,s=,T,ans=;
int num=,head[N],q[N],dep[N];
struct Lin{
int next,to,dis;
}a[N*N];
void init(int x,int y,int dis){
a[++num].next=head[x];
a[num].to=y;
a[num].dis=dis;
head[x]=num;
a[++num].next=head[y];
a[num].to=x;
a[num].dis=;
head[y]=num;
}
bool bfs()
{
memset(dep,,sizeof(dep));
q[]=s;dep[s]=;int t=,sum=,u,x;
while(t!=sum)
{
x=q[++t];
for(int i=head[x];i;i=a[i].next){
u=a[i].to;
if(dep[u]||a[i].dis<=)continue;
dep[u]=dep[x]+;q[++sum]=u;
}
}
return dep[T];
}
int dfs(int x,int flow)
{
if(x==T || !flow)return flow;
int u,tmp,sum=;
for(int i=head[x];i;i=a[i].next){
u=a[i].to;
if(a[i].dis<= || dep[u]!=dep[x]+)continue;
tmp=dfs(u,min(flow,a[i].dis));
a[i].dis-=tmp;a[i^].dis+=tmp;
sum+=tmp;flow-=tmp;
}
return sum;
}
int maxflow(){
int tot=,tmp;
while(bfs()){
tmp=dfs(s,INF);
while(tmp)tot+=tmp,tmp=dfs(s,INF);
}
return tot;
}
void work()
{
int x,k;
m=gi();n=gi();
T=n+m+;
for(int i=;i<=m;i++){
x=gi();init(s,i,x);
ans+=x;
}
for(int i=;i<=n;i++){
x=gi();init(i+m,T,x);
}
for(int i=;i<=m;i++){
k=gi();
while(k--){
x=gi();init(i,x+m+,INF);
}
}
printf("%d\n",ans-maxflow());
}
void Clear(){
memset(head,,sizeof(head));
num=;ans=;
}
int main()
{
int TT=gi();
while(TT--){
work();
Clear();
}
return ;
}
上一篇:在asp.net mvc4项目里bootstrap datetimepicker控件的使用


下一篇:LeetCode 740. Delete and Earn