PIGS
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 19711 |
|
Accepted: 9035 |
Description
Mirko works on a pig farm the consists of M locked pig-houses and Mirko can ' t unlock any pighouse because he doesn ' t has The keys. Customers come to the farm one after another. Each of them have 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 is available to Mirko early in the mornin G So, he can make a sales-plan in order to maximize the number of pigs sold.
More precisely, the procedure was as following:the customer arrives, opens all pig-houses to which he had the key, Mirko s Ells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remain ing pigs across the unlocked pig-houses.
An unlimited number of pigs can is placed in every pig-house.
Write a program, that would find the maximum number of pigs that he can sell on this day.
Input
The first line of input contains integers m and N, 1 <= m <=, 1 <= N <=, number of pighouses and Number of customers. Pig houses is numbered from 1 to M and customers is numbered from 1 to N.
The next line contains M Integeres, for each pig-house initial number of pigs. The number of pigs in pig-house are 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 on the i-th customer is written in The (i+2)-th line):
A K1 K2 ... Ka B It means that this customer have 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
Analysis
The main thing is to build a map, add a source point and a meeting point, connect the source point to the first customer with the Pigsty key, and the right side is the number of pigs in this pigsty, if the source point and a node have heavy edges, merge just
After the customer for the already opened Pigsty, the previous customer and he connected, edge is positive infinity, you can use a last array to record the purchase of this pigsty customer
Each customer is connected to a meeting point, and the right side is the number of pigs he wants to buy.
#include <stdio.h>#include<string.h>#include<iostream>#include<queue>using namespacestd;//****************************************************//maximum flow template Edmonds_karp algorithm//initialization: G[][],st,ed//******************************************************Const intMAXN = $+Ten;Const intINF =0x3fffffff;intG[MAXN][MAXN];//capacity of the storage edge, no edge initialized to 0intpath[maxn],flow[maxn],st,ed;intN//Number of points, 0~n,n includes source and sink pointsQueue<int>Q;intBFs () {inti,t; while(!q.empty ()) Q.pop ();//Empty Queuememset (path,-1,sizeof(path));//initialize the path to 1 before each searchpath[st]=0; FLOW[ST]=inf;//The source point can have infinite stream flow intoQ.push (ST); while(!Q.empty ()) {T=Q.front (); Q.pop (); if(t==ed) Break; for(i=0; i<=n;i++){ if(i!=st&&path[i]==-1&&G[t][i]) {Flow[i]=flow[t]<g[t][i]?Flow[t]:g[t][i]; Q.push (i); Path[i]=T; } } } if(path[ed]==-1)return-1;//I can't find the meeting point up there. I can't find the augmented path. returnflow[ed];}intEdmonds_karp () {intmax_flow=0; intStep,now,pre; while((Step=bfs ())!=-1) {Max_flow+=step; now=Ed; while(now!=St) {Pre=Path[now]; G[pre][now]-=step; G[now][pre]+=step; now=Pre; } } returnMax_flow;}intval[1100];intlast[1100];intMain () {intn,m; scanf ("%d%d",&m,&N); St=0, ed=n+1, n=n+1; Memset (Val,0,sizeof(Val)); Memset (Last,0,sizeof(last)); for(intI=1; i<=m;i++) scanf ("%d",&Val[i]); for(intI=1; i<=n;i++){ inta,b,v; scanf ("%d",&A); for(intj=1; j<=a;j++) {scanf ("%d",&v); if(last[v]==0) {G[st][i]+=Val[v]; LAST[V]=i; } Else{G[last[v]][i]=INF; LAST[V]=i; }} scanf ("%d",&c); G[i][ed]=C; } printf ("%d\n", Edmonds_karp ()); return 0;}
POJ 1149 PIGS (maximum flow)