# POJ 1149 PIGS

PIGS
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions:22402 Accepted: 10243

Description

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 <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;

const int MAX_D=100+5;
const int MAX_B=10000+5;
const int INF=1e9;
int v[105][105];
int vis[1000+5];
int w[1000+5];
int tot;

int iter[MAX_D];
int level[MAX_D];
struct node{
int to;
int cap;
int rev;
int next;
};
node edge[2*MAX_B];

edge[tot].to=b;
edge[tot].cap=c;
edge[tot].rev=tot+1;

edge[tot].to=a;
edge[tot].cap=0;
edge[tot].rev=tot-1;

}

void bfs(int s){
memset(level,-1,sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
node &e=edge[i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}

int dfs(int v,int t,int f){
if(v==t)return f;
for(int &i=iter[v];i!=0;i=edge[i].next){
node &e = edge[i];
if(e.cap>0&&level[v]<level[e.to]){
int d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap-=d;
edge[e.rev].cap+=d;
return d;
}
}
}

return 0;
}

int max_flow(int s,int t){
int flow=0;
while(1){
bfs(s);
if(level[t]<0)return flow;
for(int i=0;i<MAX_D;i++){
}
int f;
while((f=dfs(s,t,INF))>0){
flow+=f;
}
}
}

void solve(){
tot=1;
memset(vis,0,sizeof(vis));
int m,n;
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++){
scanf("%d",&w[i]);
}
//    cout<<endl;
//    for(int i=1;i<=m;i++){
//        cout<<w[i]<<" ";
//    }
//    cout<<endl;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
int y=0;
for(int j=1;j<=x;j++){
int z;
scanf("%d",&z);
if(vis[z]==0){
vis[z]=i+2;
y+=w[z];
}
else{
if(v[vis[z]][i+2]==0){
v[vis[z]][i+2]=1;
}
}
}
if(y>0){
}
int e;
scanf("%d",&e);
}

printf("%d",max_flow(1,2));

}

int main()
{
solve();

return 0;
}