P4016 负载平衡问题

最小费用最大流的 \(Dark\) 水题

一开始照着大佬们的建模画了一下图,如下 :

注意是一个环而,所以上面的最高的那个点要连最低的那个点的,上面没有画。

我们会发现,我们从源点给了那些点一些权值,然后让用它们的地理位置来给其它的点权值。很显然,如果要流到汇点的话,肯定要先流到那些少于平均值的点。那么这道题就搞定了。

为什么有数学 \(+\) 贪心的方法? 你没有看到这张图就是一个序列 \(+\) 两个点吗,完全可以数学啊。

Uses math;

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

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 Clear;
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);
end;

procedure Construction_I;
begin
    read(n);
    source:=1; sink:=n+2;
    for i:=1 to n do begin read(num[i]); inc(sum,num[i]); end;
    sum:=sum div n;
    for i:=1 to n do
    begin
        dec(num[i],sum);
        if num[i]>0 then add(source,i+1,num[i],0);
        if num[i]<0 then add(i+1,sink,-num[i],0);
        if i>1 then add(i+1,i,maxlongint,1);
        if i<n then add(i+1,i+2,maxlongint,1);
    end;
    add(2,n+1,maxlongint,1);
    add(n+1,2,maxlongint,1);
end;

begin
    Clear; Construction_I; MincostMaxflow; writeln(mincost);
end.
posted @ 2019-01-25 22:04  _ARFA  阅读(169)  评论(0编辑  收藏  举报