POJ 1149(构图巨难……)

内容目录

EK……

Program P1149;
var
   i,j,k,n,m,p:longint;
   map,f:array[0..500,0..500] of longint;
   visit,e,pre,pighouse:array[0..500] of longint;
   maxflow:longint;
function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;
function max(a,b:longint):longint;
begin
   if a>b then exit(a) else exit(b);
end;

procedure edmons_karp;
var
   i,j:longint;
   h,t:longint;
   queue:array[1..500] of longint;
begin
   maxflow:=0;

   while (true) do
   begin
      fillchar(pre,sizeof(pre),0);
      fillchar(e,sizeof(e),0);
      queue[1]:=0;
      e[0]:=maxlongint;
      h:=1;
      t:=1;
      while h<=t do
      begin
         for i:=1 to n+1 do
            if (map[queue[h],i]-f[queue[h],i]>0) and (e[i]=0) then
            begin
               e[i]:=min(e[queue[h]],map[queue[h],i]-f[queue[h],i]);
               pre[i]:=queue[h];
               inc(t);
               queue[t]:=i;
            end;
         if e[n+1]>0 then break;
         inc(h);
      end;

      if (e[n+1]=0) then break;

      i:=n+1;
      while (true) do
      begin
         inc(f[pre[i],i],e[n+1]);
         f[i,pre[i]]:=-f[pre[i],i];
         if i=0 then break;
         i:=pre[i];
      end;
      inc(maxflow,e[n+1]);
   end;

end;
begin
   read(m,n);
   fillchar(visit,sizeof(visit),0);
   fillchar(f,sizeof(f),0);
   fillchar(map,sizeof(map),0);
   fillchar(e,sizeof(e),0);


   for i:=1 to m do read(pighouse[i]);
   for i:=1 to n do
   begin
      read(k);
      for j:=1 to k do
      begin
         read(p);
         if (visit[p]=0) then
         begin
            map[0,i]:=map[0,i]+pighouse[p];
            visit[p]:=i;
         end
         else
         begin
            map[visit[p],i]:=maxlongint;
         end;
      end;
      read(p);
      map[i,n+1]:=p;

   end;

   edmons_karp;
   writeln(maxflow);

end.