借用Edelweiss的话,此题是典型的“蕴含式最大获利问题”,使用解决最大权闭合子图的建模方法即可解决。
每个项目i作为一个点并连边(s,i,Ai),每名员工j作为一个点并连边(j,t,Bj),若项目i需要雇佣员工j则连边(i,j,∞)。
设最小割为ans,则∑Ai - ans即为结果
代码:
#include<cstdio>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 500 + 10;
int dis[maxn],gap[maxn],pre[maxn],cur[maxn];
int cap[maxn][maxn];
int s,t,nodenum;
int T,n,m;
void init()
{
freopen("hoj2634.in","r",stdin);
freopen("hoj2634.out","w",stdout);
}
inline int min(int a,int b)
{
return a < b ? a : b;
}
int sap()
{
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
memset(pre,0,sizeof(pre));
int u = pre[s] = s,maxflow = 0,aug = inf;
gap[0] = nodenum;
while(dis[s] < nodenum)
{
loop: for(int v = cur[u];v < nodenum;v++)
{
if(cap[u][v] && dis[u] == dis[v] + 1)
{
pre[v] = u;
aug = min(aug,cap[u][v]);
u = v;
if(v == t)
{
maxflow += aug;
for(u = pre[u];v != s;v = u,u = pre[u])
{
cap[u][v] -= aug;
cap[v][u] += aug;
}
aug = inf;
}
goto loop;
}
}
int mind = nodenum;
for(int v = 0;v < nodenum;v++)
{
if(cap[u][v] && (mind > dis[v]))
{
cur[u] = v;
mind = dis[v];
}
}
if((--gap[dis[u]]) == 0)break;
gap[dis[u] = mind + 1]++;
u = pre[u];
}
return maxflow;
}
void readdata()
{
scanf("%d",&T);
while(T--)
{
memset(cap,0,sizeof(cap));
scanf("%d%d",&m,&n);
int sum = 0;
s = 0,t = n + m + 1;
nodenum = t + 1;
for(int i = 1;i <= m;i++)
{
int tmp;
scanf("%d",&tmp);
sum += tmp;
cap[s][i] = tmp;
}
for(int i = 1;i <= n;i++)
{
int tmp;
scanf("%d",&tmp);
cap[i + m][t] = tmp;
}
for(int i = 1;i <= m;i++)
{
int tot;
scanf("%d",&tot);
for(int j = 1;j <= tot;j++)
{
int tmp;
scanf("%d",&tmp);
tmp++;
cap[i][tmp + m] = inf;
}
}
int ans = sum - sap();
printf("%d\n",ans);
}
}
int main()
{
init();
readdata();
return 0;
}