且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

poj3281

更新时间:2022-09-16 20:47:54

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10843   Accepted: 4974

Description

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

Input

Line 1: Three space-separated integers: NF, and D 
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

Sample Output

3

Hint

One way to satisfy three cows is: 
Cow 1: no meal 
Cow 2: Food #2, Drink #2 
Cow 3: Food #1, Drink #1 
Cow 4: Food #3, Drink #3 
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

题目大意:有F种食物。D种饮料。N头奶牛。仅仅能吃某种食物和饮料(并且仅仅能吃特定的一份),一种食物被一头牛吃了之后。其余牛就不能吃了
第一行有N,F,D三个整数
接着2-N+1行代表第i头牛,前面两个整数是Fi与Di(食物与饮料的种类数量),接着是食物的种类与饮料的种类
要求输出最多分配可以满足的牛的数量

解题思路:建图,有2*n+f+d+2个顶点
0表示源点,2*n+f+d+1表示汇点
由源点指向食物,再由食物指向牛,牛再指向相应的饮料,饮料再指向汇点
当然要使每一头牛都相应每一份食物与饮料,所以应该牛i指向牛i再指向饮料,这样就能够避免一头牛仅仅占用多份食物与饮料了
所有是有向的边,并且权值所有为1
我在这里是1到f为食物点。f+1到f+2*n为牛点,f+2*n+1到f+2*n+d为饮料点


代码:

type 
  edge=record
    y,r,next,op:longint;
  end;
var
  g:array[0..10000] of edge;
  level,q,h:array[0..10000] of longint;
  n,m,f,i,j,a,b,c,tot,vs,ii,vt,x,y,k,ans:longint;
  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
  while not eof do
  begin
    fillchar(g,sizeof(g),0);
    fillchar(h,sizeof(h),$ff);
    fillchar(level,sizeof(level),0);
    fillchar(q,sizeof(q),0);
    readln(n,f,m);
    tot:=0;
    ans:=0;
    vs:=0;
    vt:=2*n+f+m+1;
    for i:=1 to n do
    begin
      x:=0;
      y:=0;
      a:=0;
      add(i+f,i+n+f,1);
      readln(s);
      for j:=1 to length(s) do
      begin
        if s[j]=' ' then
        break;
        x:=x*10+ord(s[j])-ord('0');
      end;
      ii:=j+1;
      for j:=ii to length(s) do
      begin
        if s[j]=' ' then
        break;
        y:=y*10+ord(s[j])-ord('0');
      end;
      k:=0;
      ii:=j+1;
      while k<x do
      begin
        if (s[ii]=' ') or (ii>length(s)) then
        begin
          add(a,f+i,1);
          a:=0;
          inc(ii);
          inc(k);
        end else
        begin
          a:=a*10+ord(s[ii])-ord('0');
          inc(ii);
        end;
      end;
      k:=0;
      while k<y do
      begin
        if (s[ii]=' ') or (ii>length(s)) then
        begin
          add(f+n+i,f+2*n+a,1);
          a:=0;
          inc(ii);
          inc(k);
        end else
        begin
          a:=a*10+ord(s[ii])-ord('0');
          inc(ii);
        end;
      end;
    end;
    for i:=1 to f do
    add(0,i,1);
    for i:=1 to m do
    add(i+2*n+f,2*n+f+m+1,1);
    while bfs do
    begin
      ans:=ans+dfs(vs,maxlongint);
    end;
    writeln(ans);
  end;
end.




本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5379997.html,如需转载请自行联系原作者

相关阅读