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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值