AC代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
const int N=1020;
int e[N][N],house[N],pre[N];
int m,n;
int s,t;
bool book[N];
int maxflow;
bool bfs()
{
memset(book,false,sizeof(book));
memset(pre,0,sizeof(pre));
queue<int>Q;
Q.push(s);
book[s]=true;
while(!Q.empty())
{
int p=Q.front();
Q.pop();
if(p==t)
return true;
for(int i=1; i<=t; i++)
{
if(book[i]==false)
{
if(e[p][i]>0)
{
book[i]=true;
pre[i]=p;
Q.push(i);
}
}
}
}
return false;
}
int solve()
{
maxflow=0;
while(1)
{
if(bfs()==false)
return maxflow;
int minn=inf;
for(int i=t; i!=s; i=pre[i])
minn=min(minn,e[pre[i]][i]);
for(int i=t; i!=s; i=pre[i])
{
e[pre[i]][i]-=minn;
e[i][pre[i]]+=minn;
}
maxflow+=minn;
}
}
int main()
{
int key,num;
while(~scanf("%d %d",&m,&n))
{
memset(pre,0,sizeof(pre));
memset(e,0,sizeof(e));
s=0,t=n+1;
for(int i=1; i<=m; i++)
scanf("%d",&house[i]);
for(int i=1; i<=n; i++) //构造网络流
{
scanf("%d",&key);//读入钥匙总数
for(int j=1; j<=key; j++)
{
scanf("%d",&num);//读入每个钥匙能够打开的猪圈序号
if(pre[num]==0)//第i个顾客是第num个猪圈的第一个顾客
e[s][i]+=house[num];//即该猪圈之前没有人要过这里面的猪
else
e[pre[num]][i]=inf;
pre[num]=i;
}
scanf("%d",&e[i][t]);//每个顾客需要买多少只猪
}
printf("%d
",solve());
}
return 0;
}