Mirko works on a pig farm that consists of M locked pig-houses and Mirko can’t unlock any pighouse because he doesn’t have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs.
All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold.
More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses.
An unlimited number of pigs can be placed in every pig-house.
Write a program that will find the maximum number of pigs that he can sell on that day.
Input
The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N.
The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000.
The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line):
A K1 K2 … KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, …, KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.
Output
The first and only line of the output should contain the number of sold pigs.
Sample Input
3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6
Sample Output
7
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 1010
#define INF 9999999
int map[MAXN][MAXN];
int f[MAXN];
int pig[MAXN];
int book[MAXN];
int a[MAXN];
int EK(int n)
{
int i,sum=0,flog;
while(1)
{
memset(a,0,sizeof(a));
memset(book,0,sizeof(book));
queue<int> q;
f[0]=INF;
q.push(0);
flog=0;
while(q.size()&&!flog)
{
int k=q.front();
q.pop();
for(i=1; i<=n; i++)
{
if(book[i]==0&&map[k][i]>0)
{
book[i]=1;
a[i]=k;
f[i]=min(map[k][i],f[k]);
q.push(i);
if(i==n)
{
flog=1;
break;
}
}
}
}
if(flog==0)
break;
for(i=n; i!=0; i=a[i])
{
int l=a[i];
map[l][i]-=f[n];
map[i][l]+=f[n];
}
sum=sum+f[n];
}
printf("%d\n",sum);
}
int main()
{
int m,n,i;
int p[MAXN];
while(~scanf("%d %d",&m,&n))
{
memset(p,0,sizeof(p));
memset(map,0,sizeof(map));
int a,b,l,k;
for(i=1; i<=m; i++)
scanf("%d",&pig[i]);
for(i=1; i<=n; i++)// 建 图
{
scanf("%d",&l);
while(l--)
{
scanf("%d",&k);
if(p[k]==0)
{
map[0][i]+=pig[k];
p[k]=i;
}
else
{
map[p[k]][i]=INF;
p[k]=i;
}
}
scanf("%d",&k);
map[i][n+1]=k;
}
EK(n+1);
}
}