登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

z55250825

一只蒟蒻

 
 
 

日志

 
 

【网络流24题】【费用流】【分配问题】  

2014-01-29 15:28:18|  分类: AC题目 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
   也是裸的费用流,每一个人与源点连容量为1,费用为0 的边,然后每一项工作同左,连到汇点,然后对于 第I个人做第j份工作,连 容量为1,费用为 Cij 的边,然后跑费用流即可...这里直接复制运输问题模板上来了...
===================================

program flow18;
const maxn=10000;
type
  lin=record
  u,v,w,c,pre,cap:longint;
  end;

var
  e:array[0..10000]of lin;
  low,pre,d,last:array[0..110]of longint;
  v:array[0..110]of boolean;
  que:array[0..maxn]of longint;
  n,m,cost,tot:longint;

function min(a,b:longint):longint;
begin
  if a<b then exit(a) else exit(b);
end;

procedure addedge(u,v,w,c,cap:longint);
begin
  inc(tot);
  e[tot].u:=u;
  e[tot].v:=v;
  e[tot].w:=w;
  e[tot].c:=c;
  e[tot].cap:=cap;
  e[tot].pre:=last[u];
  last[u]:=tot;
end;

function spfa(s,t:longint):boolean;
var l,r,tmp,aug,u:longint;
begin
  fillchar(que,sizeof(que),0);
  fillchar(low,sizeof(low),0);
  fillchar(pre,sizeof(pre),0);
  fillchar(v,sizeof(v),false);
  fillchar(d,sizeof(d),1);
  l:=0;r:=1;que[l]:=s;
  d[s]:=0;v[s]:=true;
  low[s]:=maxlongint;low[t]:=low[s];
  while l<>r do
   begin
     u:=que[l];
     tmp:=last[u];
     while tmp<>-1 do
      begin
        if (d[e[tmp].v]>d[u]+e[tmp].c)and(e[tmp].w>0) then
         begin
           d[e[tmp].v]:=d[u]+e[tmp].c;
           pre[e[tmp].v]:=tmp;
           low[e[tmp].v]:=min(low[u],e[tmp].w);
           if not v[e[tmp].v] then
            begin
              que[r]:=e[tmp].v;
              r:=(r+1)mod maxn;
              v[e[tmp].v]:=true;
            end;
         end;
        tmp:=e[tmp].pre;
      end;
     v[u]:=false;
     l:=(l+1)mod maxn;
   end;
  if low[t]=maxlongint then exit(false);
  cost:=cost+low[t]*d[t];
  u:=t;aug:=low[t];
  while u<>s do
   begin
     dec(e[pre[u]].w,aug);
     inc(e[pre[u] xor 1].w,aug);
     u:=e[pre[u]].u;
   end;
  exit(true);
end;

procedure init;
var i,j,k:longint;
begin
  read(n);
  fillchar(last,sizeof(last),255);
  tot:=-1;
  for i:=1 to n do
   begin
     addedge(0,i,1,0,1);
     addedge(i,0,0,0,0);
   end;
  for i:=1 to n do
   begin
     addedge(i+n,n+n+1,1,0,1);
     addedge(n+n+1,i+n,0,0,0);
   end;
  for i:=1 to n do
    for j:=1 to n do
      begin
        read(k);
        addedge(i,j+n,1,k,1);
        addedge(j+n,i,0,-k,0);
      end;
  cost:=0;
  while spfa(0,n+n+1) do;
  writeln(cost);
  cost:=0;
  for i:=0 to tot do
    begin
      e[i].w:=e[i].cap;
      e[i].c:=-e[i].c;
    end;
  while spfa(0,n+n+1) do;
  writeln(-cost);
end;

begin
  init;
end.



  评论这张
 
阅读(70)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018