P4014 分配问题

一个带权值的二分图。

注意源点和左边的点连还有右边的点向汇点连的时候流量为 \(1\) (不能被重复匹配) 就可以了。

本蒟蒻不会 \(KM\)

// luogu-judger-enable-o2
Uses math;

var
    from,reach,next,value,cost:array[-1..50010] of longint;
    dis,pre,last,flow:array[-1..50010] of int64;
    queue:array[-1..50010] of longint;
    cnt:array[-1..8100] of longint;
    vis:array[-1..8100] of boolean;
    val:array[-1..101,-1..101] of longint;
    n,m,i,j,k,tot,now,sink,source,maxflow,mincost:longint;

procedure add(x,y,sum_1,sum_2:longint);
begin
    inc(tot); from[tot]:=x; reach[tot]:=y; value[tot]:=sum_1; cost[tot]:= sum_2; next[tot]:=cnt[x]; cnt[x]:=tot;
    inc(tot); from[tot]:=y; reach[tot]:=x; value[tot]:=0    ; cost[tot]:=-sum_2; next[tot]:=cnt[y]; cnt[y]:=tot;
end;

function spfa:boolean;
var head,tail,now,i:longint;
begin
    filldword(dis,sizeof(dis) div 4,maxlongint);
    filldword(flow,sizeof(flow) div 4,maxlongint);
    filldword(vis,sizeof(vis) div 4,0);
    head:=1; tail:=1;  queue[1]:=source; vis[source]:=True; dis[source]:=0; pre[sink]:=-1;

    while head<=tail do
    begin
        now:=queue[head]; vis[now]:=False; inc(head);
        i:=cnt[now];
        while i<>-1 do
        begin
            if (value[i]>0)and(dis[reach[i]]>dis[now]+cost[i]) then
            begin
                dis[reach[i]]:=dis[now]+cost[i];
                pre[reach[i]]:=now;
                last[reach[i]]:=i;
                flow[reach[i]]:=min(flow[now],value[i]);
                if vis[reach[i]]=False then
                begin
                    vis[reach[i]]:=True;
                    inc(tail); queue[tail]:=reach[i];
                end;
            end;
            i:=next[i];
        end;
    end;
    if pre[sink]=-1 then exit(False); exit(True);
end;

procedure MincostMaxflow;
begin
    maxflow:=0; mincost:=0; now:=0;
    while (spfa) do
    begin
        now:=sink;
        inc(maxflow,flow[sink]);
        inc(mincost,flow[sink]*dis[sink]);
        while now<>source do
        begin
            dec(value[last[now]],flow[sink]);
            inc(value[last[now] xor 1],flow[sink]);
            now:=pre[now];
        end;
    end;
end;

procedure Construction_I;
begin
    filldword(cnt,sizeof(cnt) div 4,maxlongint*2+1); tot:=1;
    fillchar(value,sizeof(value),0);
    fillchar(reach,sizeof(reach),0);
    fillchar(cost,sizeof(cost),0);
    fillchar(next,sizeof(next),0);
   	for i:=1 to n do add(source,i+1,1,0);
   	for i:=1 to n do add(i+n+1,sink,1,0);
    for i:=1 to n do for j:=1 to n do begin read(val[i,j]); add(i+1,j+n+1,1,val[i,j]); end;
end;

procedure Construction_II;
begin
    filldword(cnt,sizeof(cnt) div 4,maxlongint*2+1); tot:=1;
    fillchar(value,sizeof(value),0);
    fillchar(reach,sizeof(reach),0);
    fillchar(cost,sizeof(cost),0);
    fillchar(next,sizeof(next),0);
   	for i:=1 to n do add(source,i+1,1,0);
   	for i:=1 to n do add(i+n+1,sink,1,0);
  	for i:=1 to n do for j:=1 to n do add(i+1,j+n+1,1,-val[i,j]);
end;


begin
    read(n); source:=1; sink:=(n+2) << 1;
    Construction_I ; MincostMaxflow; writeln( mincost);
    Construction_II; MincostMaxflow; writeln(-mincost);
end.
posted @ 2019-01-25 22:00  _ARFA  阅读(137)  评论(0编辑  收藏  举报