bzoj 1497 最小割模型

我们可以对于消费和盈利的点建立二分图,开始答案为所有的盈利和,

那么源向消费的点连边,流量为消费值,盈利向汇连边,流量为盈利值

中间盈利对应的消费连边,流量为INF,那么我们求这张图的最小割,用

开始的答案减去最小割就是答案,因为最小割的存在不是左面就是右面,

割左面,代表建这条路,需要对应的消费,那么割右面代表不要这项盈利,

那本来加进去的盈利应该减掉,所以可以这样更新答案。

/**************************************************************
    Problem:
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time: ms
    Memory: kb
****************************************************************/
 
//By BLADEVIL
var
    n, m                        :longint;
    pre, other, len             :array[..] of longint;
    last                        :array[..] of longint;
    l                           :longint;
    source, sink                :longint;
    ans                         :longint;
    que, d                      :array[..] of longint;
     
function min(a,b:longint):longint;
begin
    if a>b then min:=b else min:=a;
end;
 
procedure connect(x,y,z:longint);
begin
    inc(l);
    pre[l]:=last[x];
    last[x]:=l;
    other[l]:=y;
    len[l]:=z;
end;
     
procedure init;
var
    i                           :longint;
    x, y, z                     :longint;
begin
    read(n,m); source:=n+m+; sink:=source+;
    l:=;
    for i:= to n do
    begin
        read(x);
        connect(source,i,x);
        connect(i,source,);
    end;
    for i:=n+ to n+m do
    begin
        read(x,y,z);
        connect(x,i,maxlongint div );
        connect(i,x,);
        connect(y,i,maxlongint div );
        connect(i,y,);
        connect(i,sink,z);
        connect(sink,i,);
        ans:=ans+z;
    end;
end;
 
function bfs:boolean;
var
    q, p, cur                   :longint;
    h, t                        :longint;
begin
    fillchar(d,sizeof(d),);
    h:=; t:=; d[source]:=;
    que[]:=source;
    while h<t do
    begin
        inc(h);
        cur:=que[h];
        q:=last[cur];
        while q<> do
        begin
            p:=other[q];
            if (len[q]>) and (d[p]=) then
            begin
                inc(t);
                que[t]:=p;
                d[p]:=d[cur]+;
                if p=sink then exit(true);
            end;
            q:=pre[q];
        end;
    end;
    exit(false);
end;
 
function dinic(x,flow:longint):longint;
var
    tmp, rest                   :longint;
    q, p                        :longint;
begin
    if x=sink then exit(flow);
    rest:=flow;
    q:=last[x];
    while q<> do
    begin
        p:=other[q];
        if (len[q]>) and (d[p]=d[x]+) and (rest>) then
        begin
            tmp:=dinic(p,min(len[q],rest));
            dec(rest,tmp);
            dec(len[q],tmp);
            inc(len[q xor ],tmp);
        end;
        q:=pre[q];
    end;
    exit(flow-rest);
end;
 
procedure main;
begin
    while bfs do ans:=ans-dinic(source,maxlongint div );
    writeln(ans);
end;
 
begin
    init;
    main;
end.
上一篇:C# 日期格式大全


下一篇:IIS 禁止回收