poj1149

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17919 Accepted: 8149

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
 
 
题意: 有 M 个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依次来了 N 个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。


分析: 此题是网络流,网络流里经典的构图题。将顾客看作除源和汇以外的节点,源和每个猪圈的第一个顾客连边,边的权是开始时猪圈中猪的数目,若源和某个节点之间有重边,则将权合并,每个猪圈的前后相邻两顾客之间连边,由前一个顾客指向后一个顾客。每个顾客和汇之间连边,边的权是顾客所希望购买的猪的数目。
网络流,需要对图进行化简
化简规则:
规律 1. 如果几个节点的流量的来源完全相同,且流量为+∞,则可以把它们合并成一个。
规律 2. 如果几个节点的流量的去向完全相同,且流量为+∞,则可以把它们合并成一个。
规律 3. 如果从点 u 到点 v 有一条容量为 +∞ 的边,并且 u 是 v 的唯一流量来源,或者 v 是 u 的唯一流量去向,则可以把 u 和 v 合并成一个节点。


代码:
type
  edge=record
    y,r,next,op:longint;
  end;
var
  g:array[0..10000] of edge;
  level,q,h,pig,c,peo:array[0..10010] of longint;
  cus:array [1..1010,1..1010] of longint;
  n,m,f,i,j,a,b,tot,vs,ii,vt,x,y,k:longint;
  ans:qword;
  s:string;
function bfs:boolean;
var
  i,f,r,tmp,v,u:longint;
begin
  fillchar(level,sizeof(level),0);
  f:=1;  r:=1;
  q[f]:=vs;  level[vs]:=1;
  repeat
    v:=q[f];    tmp:=h[v];
    while tmp<>-1 do
      begin
        u:=g[tmp].y;
        if (g[tmp].r<>0) and (level[u]=0) then
          begin
            level[u]:=level[v]+1;
            inc(r);    q[r]:=u;
            if u=vt then exit(true);
          end;
        tmp:=g[tmp].next;
      end;
    inc(f);
  until f>r;
  exit(false);
end;
function min(x,y:longint):longint;
begin
  if x<y then
  exit(x);
  exit(y);
end;
function dfs(v,a:longint):longint;
var
  ans,flow,tmp,u,value:longint;
begin
  if (v=vt) or (a=0) then exit(a);
  ans:=0;  tmp:=h[v];
  while tmp<>-1 do
    begin
      u:=g[tmp].y;    value:=g[tmp].r;
      if (level[u]=level[v]+1)  then
        begin
          flow:=dfs(u,min(a,value));
          if flow<>0 then
            begin
              g[tmp].r:=g[tmp].r-flow;
              g[g[tmp].op].r:=g[g[tmp].op].r+flow;
              ans:=ans+flow;
              a:=a-flow;
              if a=0 then break;
            end;
        end;
      tmp:=g[tmp].next;
    end;
  exit(ans);
end;
procedure add(a,b,c:longint);
begin
  inc(tot);
  g[tot].y:=b;
  g[tot].r:=c;
  g[tot].next:=h[a];
  h[a]:=tot;
  g[tot].op:=tot+1;
  inc(tot);
  g[tot].y:=a;
  g[tot].r:=0;
  g[tot].next:=h[b];
  h[b]:=tot;
  g[tot].op:=tot-1;
end;




begin
    fillchar(h,sizeof(h),$ff);
    readln(n,m);
    tot:=0;
    ans:=0;
    vs:=0;
    vt:=m+1;
    for i:=1 to n do
    read(pig[i]);
    readln;
    for i:=1 to m do
    begin
      read(x);
      for j:=1 to x do
      begin
        read(y);
        inc(c[y]);
        cus[y,c[y]]:=i;
      end;
      read(y);
      readln; 
      peo[i]:=y;
    end;
    for i:=1 to n do
    begin
      add(vs,cus[i,1],pig[i]);
      for j:=2 to c[i] do
      add(cus[i,j-1],cus[i,j],maxlongint);
    end;
    for i:=1 to m do
    add(i,vt,peo[i]);
    while bfs do
    begin
      ans:=ans+dfs(vs,maxlongint);
    end;
    writeln(ans);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值