Time Limit:1000MS Memory Limit:10000K
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.
迈克在一个养猪场工作,养猪场里有M个猪圈,每个猪圈都上了锁。由于迈克没有钥匙,所以他不能打开任何一个猪圈。要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的猪。某一天,所有要到养猪场买猪的顾客,他们的信息是要提前让迈克知道的。这些信息包括:顾客所拥有的钥匙(详细到有几个猪圈的钥匙、有哪几个猪圈的钥匙)、要购买的数量。这样对迈克很有好处。他可以安排销售计划以便卖出的猪的数目最大。
更详细的销售过程为:当每个顾客到来时,他将那些他拥有钥匙的猪圈全部打开;迈克从这些猪圈中挑出一些猪卖给他们;如果迈克愿意,迈克可以重新分配这些被打开猪圈的书;当顾客离开时,猪圈再次被锁上。注意:猪圈可容纳的猪的数量没有限制。
编写程序,计算迈克这一天能卖出猪的最大数目。
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.
输入格式如下:
(1)第1行是两个整数,M和N(1<=M<=1000,1<=N<=100)。M是猪圈的数目,N是顾客的数目。猪圈的编号从1到M,顾客的编号从1到N。
(2)第二行是M个整数,为每个猪圈中初始时猪的数目,范围是[0,1000]。
(3)接下来的N行是顾客的信息,第i个顾客的信息保存在第i+2行,格式为:A K1 K2 ... KA B。A为拥有钥匙的数目,Kj表示有第Kj个猪圈的钥匙,B为该顾客想买的猪的数目。A、B均可为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
Source
Croatia OI 2002 Final Exam - First day
代码:
1 var 2 a,b,c,e,n,m,i,j,s,t,tmp:longint; 3 ans,inf:int64; 4 pig,shop,d,h,g,f:array[0..1001]of longint; 5 ot,cap,ne:array[0..101*101]of longint; 6 tt:array[1..100,1..100]of boolean; 7 8 procedure addedge(x,y,z:longint); 9 begin 10 ot[e]:=y; ne[e]:=g[x]; cap[e]:=z; g[x]:=e; inc(e); 11 ot[e]:=x; ne[e]:=g[y]; cap[e]:=0; g[y]:=e; inc(e); 12 end; 13 14 function min(a,b:int64):int64; 15 begin 16 if a<b then exit(a) else exit(b); 17 end; 18 19 function bfs:boolean; 20 var 21 l,r,p:int64; 22 begin 23 for i:=s to t do d[i]:=n+10; 24 l:=0; r:=1; h[l]:=s; d[s]:=0; 25 while l<r do 26 begin 27 inc(l); 28 p:=g[h[l]]; 29 while p<>-1 do 30 begin 31 if (cap[p]<>0)and(d[ot[p]]>d[h[l]]+1) then 32 begin 33 inc(r); 34 h[r]:=ot[p]; 35 d[ot[p]]:=d[h[l]]+1; 36 end; 37 p:=ne[p]; 38 end; 39 end; 40 exit(d[t]<>n+10); 41 end; 42 43 function dfs(x,flow:int64):int64; 44 var 45 p,tmp:int64; 46 begin 47 if x=t then exit(flow); 48 p:=f[x]; dfs:=0; 49 while p<>-1 do 50 begin 51 if (cap[p]>0)and(d[ot[p]]=d[x]+1) then 52 begin 53 tmp:=dfs(ot[p],min(flow-dfs,cap[p])); 54 inc(dfs,tmp); 55 inc(cap[p xor 1],tmp); 56 dec(cap[p],tmp); 57 end; 58 p:=ne[p]; 59 end; 60 f[x]:=p; 61 end; 62 63 procedure ready; 64 begin 65 readln(m,n); 66 fillchar(g,sizeof(g),255); 67 fillchar(shop,sizeof(shop),0); 68 fillchar(tt,sizeof(tt),false); 69 e:=0; 70 for i:=1 to m do 71 read(pig[i]); 72 for i:=1 to n do 73 begin 74 read(a); tmp:=0; 75 for j:=1 to a do 76 begin 77 read(c); 78 if shop[c]=0 79 then begin 80 shop[c]:=i; 81 inc(tmp,pig[c]); 82 end 83 else begin 84 if not tt[shop[c],i] 85 then begin 86 addedge(shop[c],i,maxlongint); 87 tt[shop[c],i]:=true; 88 end; 89 shop[c]:=i; 90 end; 91 end; 92 if tmp>0 then addedge(0,i,tmp); 93 readln(b); 94 addedge(i,n+1,b); 95 end; 96 s:=0; t:=n+1; ans:=0; 97 end; 98 99 begin 100 inf:=high(int64); 101 while not eof do begin 102 ready; 103 while bfs do 104 begin 105 for i:=s to t do f[i]:=g[i]; 106 inc(ans,dfs(s,inf)); 107 end; 108 writeln(ans); end; 109 end.