• POJ 1149 -PIGS


     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

        

    Source

     Croatia OI 2002 Final Exam - First day

     

        本题的关键在于如何构造一个容量网络。在本题中,容量网络的构造方法如下。
       (1)将顾客看作除源点和汇点以外的结点,并且另外设两个结点:源点和汇点。
       (2)源点和每个猪圈的第一个顾客连边,边的权是开始时猪圈中猪的数目。
       (3)若源点和某个结点之间有重边,则将权合并(因此源点流出的流量就是所有猪圈能提供的猪的数目)
       (4)顾客j紧跟顾客i之后打开某个猪圈,则边<i,j>的权是+∞;这是因为,如果顾客j紧跟顾客i之后打开某个猪圈,那么Mike就有可能根据顾客j的需求将其他猪圈中的猪调整到该猪圈,这样顾客j就能买到尽可能多的猪。
       (5)每个顾客和汇点之间连边,边的权是顾客所希望购买的猪的数目(因此汇点的流入量就是每个顾客所购买的猪的个数)

        代码:

      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.
  • 相关阅读:
    解决虚拟机中使用ntpdate报错:ntpdate[46700]: no server suitable for synchronization found
    Hive的三种安装方式(内嵌模式,本地模式远程模式)
    攻城狮在路上(陆)-- 提交运行MapReduce程序到hadoop集群运行
    攻城狮在路上(陆)-- 配置hadoop本地windows运行MapReduce程序环境
    攻城狮在路上(陆)-- hadoop分布式环境搭建(HA模式)
    攻城狮在路上(陆)-- hadoop单机环境搭建(一)
    Linux下Keepalived+LVS-DR模式配置高可用负载均衡集群
    Git版本控制管理学习笔记5-提交
    Git版本控制管理学习笔记4-文件管理和索引
    Git版本控制管理学习笔记3-基本的Git概念
  • 原文地址:https://www.cnblogs.com/kry-ssw-1314/p/4559314.html
Copyright © 2020-2023  润新知