首页 > 代码库 > POJ 1149 PIGS

POJ 1149 PIGS

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20892 Accepted: 9549

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 33 1 102 1 2 22 1 3 31 2 6

Sample Output

7

Source

Croatia OI 2002 Final Exam - First day
 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 #include<vector> 6 #define maxn 1050 7 using namespace std; 8 int dep[110],m,n,pig[maxn],house[maxn]; 9 int vis[110],map[110][110];10 #define INF 0x7fffffff11 bool BFS(){12     memset(dep,-1,sizeof(dep));13     queue<int>q;14     dep[0]=0;q.push(0);15     while(!q.empty()){16         int u=q.front();q.pop();17         for(int v=1;v<=n+1;v++){18             if(map[u][v]>0&&dep[v]==-1){19                 dep[v]=dep[u]+1;20                 if(v==n+1)return 1;21                 else q.push(v);22             }23         }24     }25     return 0;26 }27 int Dinic(){28     int maxf=0;vector<int>q;29     while(BFS()){30         memset(vis,0,sizeof(vis));31         q.push_back(0);vis[0]=1;32         while(!q.empty()){33             int p=q.back();34             if(p==n+1){35                 int minn,minx=0x7fffffff;36                 for(int i=1;i<q.size();i++){37                     int u=q[i-1],v=q[i];38                     if(map[u][v]<minx){39                         minx=map[u][v];40                         minn=u;41                     }42                 }43                 maxf+=minx;44                 for(int i=1;i<q.size();i++){45                     int u=q[i-1],v=q[i];46                     map[u][v]-=minx;map[v][u]+=minx;47                 }48                 while(!q.empty()&&q.back()!=minn){49                     vis[q.back()]=0;q.pop_back();50                 }51             }52             else {53                 int i;54                 for(i=0;i<=n+1;i++){55                     if(map[p][i]>0&&dep[i]==dep[p]+156                         &&!vis[i]){57                         q.push_back(i);vis[i]=1;58                         break;59                     }60                 }61                 if(i>n+1)q.pop_back();62             }63         }64     }65     return maxf;66 }67 int main(){68     cin>>m>>n;69     for(int i=1;i<=m;i++)70       scanf("%d",pig+i);71     72     int num,k;73     for(int i=1;i<=n;i++){74         scanf("%d",&num);75         for(int j=0;j<num;j++){76             scanf("%d",&k);77             if(house[k]==0) map[0][i]+=pig[k];78             else map[house[k]][i]=INF;79             house[k]=i;80         }81         int tpig;82         scanf("%d",&tpig);83         map[i][n+1]=tpig;84     }85     cout<<Dinic()<<endl;86     return 0;87 }

 

POJ 1149 PIGS