HOJ 2634 How to earn more

How to earn more

My Tags   (Edit)
  Source : ww
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 381, Accepted : 217

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 ithnumber 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.

题意:

有m个项目,n个员工,每个项目可以得到一些收益,每个员工需要给付一些工资,每个项目对应若干个员工,每个员工可以参加多个项目,求出最大收益。

分析:

这道题可以转化为最大权闭合子图,项目看成权值为正的点,员工看成权值为负的点,每个项目对应若干个员工也就是说如果要选择这个项目就必须要选择这些员工,也就是项目向员工连边,出边必须在点集中,然后求最小割就好了...

代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 //by NeighThorn
 6 #define inf 0x3f3f3f3f
 7 using namespace std;
 8
 9 const int maxn=200+2+5,maxm=40000+5;
10
11 int cas,m,n,ans;
12 int hd[maxn],fl[maxm],to[maxm],nxt[maxm],pos[maxn],S,T,cnt;
13
14 inline int read(void){
15     char ch=getchar();int x=0;
16     while(!(ch>=‘0‘&&ch<=‘9‘))
17         ch=getchar();
18     while(ch>=‘0‘&&ch<=‘9‘)
19         x=x*10+ch-‘0‘,ch=getchar();
20     return x;
21 }
22
23 inline void add(int s,int x,int y){
24     fl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
25     fl[cnt]=0;to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++;
26 }
27
28 inline bool bfs(void){
29     memset(pos,-1,sizeof(pos));
30     int q[maxn],head=0,tail=0;
31     q[0]=S,pos[S]=0;
32     while(head<=tail){
33         int top=q[head++];
34         for(int i=hd[top];i!=-1;i=nxt[i])
35             if(pos[to[i]]==-1&&fl[i])
36                 pos[to[i]]=pos[top]+1,q[++tail]=to[i];
37     }
38     return pos[T]!=-1;
39 }
40
41 inline int find(int v,int f){
42     if(v==T)
43         return f;
44     int res=0,t;
45     for(int i=hd[v];i!=-1&&res<f;i=nxt[i])
46         if(pos[to[i]]==pos[v]+1&&fl[i])
47             t=find(to[i],min(f-res,fl[i])),res+=t,fl[i]-=t,fl[i^1]+=t;
48     if(!res)
49         pos[v]=-1;
50     return res;
51 }
52
53 inline int dinic(void){
54     int res=0,t;
55     while(bfs())
56         while(t=find(S,inf))
57             res+=t;
58     return res;
59 }
60
61 signed main(void){
62     cas=read();
63     while(cas--){
64         memset(hd,-1,sizeof(hd));
65         ans=cnt=0;m=read(),n=read(),S=0,T=m+n+1;
66         for(int i=1,x;i<=m;i++)
67             x=read(),ans+=x,add(x,S,i);
68         for(int i=1,x;i<=n;i++)
69             x=read(),add(x,i+m,T);
70         for(int i=1,num;i<=m;i++){
71             num=read();
72             for(int j=1,x;j<=num;j++)
73                 x=read(),x++,add(inf,i,x+m);
74         }
75         ans=ans-dinic();printf("%d\n",ans);
76     }
77     return 0;
78 }



by NeighThorn

时间: 2024-10-01 05:20:26

HOJ 2634 How to earn more的相关文章

hoj 2634

[题目大意]有M个项目和N个员工.做项目i可以获得Ai元,但是必须雇用若干个指定的员工.雇用员工j需要花费Bj元,且一旦雇用,员工j可以参加多个项目的开发.问经过合理的项目取舍,最多能挣多少钱.(1 <= M, N <= 100) 蕴含式最大获利问题..建出最大权闭合子图,答案为sum(a[i])-最小割 1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<cstring> 4 #include

网络流专栏

最大流 POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall (二分图匹配) POJ 1698 Alice's Chance(构图) POJ 1459 Power Network(构图) POJ 2112 Optimal Milking (二分) POJ 2455 Secret Milking Machine (二分) POJ 3189 Steady Cow Assignment (枚举) POJ 1637 Sightseeing tour (

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

网络流柱

最大流量 POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall (二分图匹配) POJ 1698 Alice's Chance(构图) POJ 1459 Power Network(构图) POJ 2112 Optimal Milking (二分) POJ 2455 Secret Milking Machine (二分) POJ 3189 Steady Cow Assignment (枚举) POJ 1637 Sightseeing tour

待刷题目分类

各大OJ题目归类 Posted on 2012 年 8 月 8 日 by admin ---------–最优化问题------------- --------动态规划 SOJ1162 I-Keyboard SOJ2096 Maximum Submatrix SOJ2111 littleken bg SOJ2505 The County Fair SOJ2818 QQ音速 SOJ2469 Exploring Pyramids SOJ1833 Base Numbers SOJ2009 Zeros

网络流题集

最大流POJ 1273 Drainage DitchesPOJ 1274 The Perfect Stall (二分图匹配)POJ 1698 Alice's Chance(构图)POJ 1459 Power Network(构图)POJ 2112 Optimal Milking (二分)POJ 2455 Secret Milking Machine (二分)POJ 3189 Steady Cow Assignment (枚举)POJ 1637 Sightseeing tour (混合图欧拉回路)

最大权闭合子图(最小割)

最大权闭合子图(最大流最小割) •参考资料 [1]最大权闭合子图 •权闭合子图 存在一个图的子图,使得子图中的所有点出度指向的点依旧在这个子图内,则此子图是闭合子图. 在这个图中有8个闭合子图:∅,{3},{4},{2,4},{3,4},{1,3,4},{2,3,4},{1,2,3,4} •最大权闭合子图 在一个图中每个点具有点权值,在他的所有闭合子途中点权之和最大的即是最大权闭合子图. •详解 最大权闭合子图 结论 最大权闭合子图权值  =  所有权值为正的权值之和  -  最大流 •步骤 建

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

HOJ 1797 Red and Black

传送门  http://acm.hit.edu.cn/hoj/problem/view?id=1797 总体的思路是遍历可以到达的' . ',将其对应的vis数组化为1,然后统计所有为1的vis项; ①常用的加边法,防止越界 ②初始化,不然两次相同的输入得到的结果会不同,由于是二维数组,能力有限,只好在结尾初始化,为下次输入做准备 ③结束条件,递归函数一定要先写好结束条件,不然会炸 ④上下左右四向移动 ⑤直接递归函数调用自身 1 #include <stdio.h> 2 #include &l