详见BYV的博客,写的非常全面https://www.byvoid.com/blog/noi-2008-employee

/**************************************************************
    Problem:
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time: ms
    Memory: kb
****************************************************************/
 
//By BLADEVIL
var
    n, m                        :longint;
    pre, other, len, cost       :array[..] of longint;
    last                        :array[..] of longint;
    l                           :longint;
    source, sink                :longint;
    ans                         :longint;
    flag                        :array[..] of boolean;
    dis, que, father            :array[..] of longint;
     
function min(a,b:longint):longint;
begin
    if a>b then min:=b else min:=a;
end;
     
procedure connect(a,b,c,d:longint);
begin  
    inc(l);
    pre[l]:=last[a];
    last[a]:=l;
    other[l]:=b;
    len[l]:=c;
    cost[l]:=d;
end;
     
procedure init;
var
    i                           :longint;
    x, y, z                     :longint;
     
begin
    read(n,m);
    source:=n+; sink:=source+;
    x:=;
    l:=;
    for i:= to n do
    begin
        read(y);
        if y-x> then
        begin
            connect(source,i,y-x,);
            connect(i,source,,);
        end else
        begin
            connect(i,sink,x-y,);
            connect(sink,i,,);
        end;
        x:=y;
    end;
    connect(n+,sink,x,);
    connect(sink,n+,,);
    for i:= to m do
    begin
        read(x,y,z);
        connect(x,y+,maxlongint div ,z);
        connect(y+,x,,-z);
    end;
    for i:= to n+ do
    begin
        connect(i,i-,maxlongint div ,);
        connect(i-,i,,);
    end;
end;
 
function spfa:boolean;
var
    h, t, cur                   :longint;
    q, p                        :longint;
begin
    filldword(dis,sizeof(dis) div ,maxlongint div );
    que[]:=source;
    dis[source]:=;
    h:=; t:=;
    while t<>h do
    begin
        h:=h mod +;
        cur:=que[h];
        flag[cur]:=false;
        q:=last[cur];
        while q<> do
        begin
            p:=other[q];
            if len[q]> then
                if dis[p]>dis[cur]+cost[q] then
                begin
                    father[p]:=q;
                    dis[p]:=dis[cur]+cost[q];
                    if not flag[p] then
                    begin
                        t:=t mod +;
                        que[t]:=p;
                        flag[p]:=true;
                    end;
                end;
            q:=pre[q];
        end;
    end;
    if dis[sink]=maxlongint div then exit(false) else exit(true);
end;
 
procedure update;
var
    low, cur                    :longint;
begin
    cur:=sink;
    low:=maxlongint;
    while cur<>source do
    begin
        low:=min(low,len[father[cur]]);
        cur:=other[father[cur] xor ];
    end;
    cur:=sink;
    while cur<>source do
    begin
        dec(len[father[cur]],low);
        inc(len[father[cur] xor ],low);
        ans:=ans+low*cost[father[cur]];
        cur:=other[father[cur] xor ];
    end;
end;
 
procedure main;
begin
    while spfa do
        update;
    writeln(ans);
end;
 
begin
    init;
    main;
end.

最新文章

  1. MVC 外网 上传 下载 实现方式(一)
  2. app启动速度
  3. spring3.0事务的配置
  4. GitHub学习笔记
  5. IAsyncResult 接口异步 和匿名委托
  6. 班级博客客户端Beta阶段发布说明
  7. javaweb学习总结(四)——Http协议(转)
  8. 工厂模式讲解, 引入Spring IOC
  9. jmeter和jdk的安装教程
  10. iOS相关的ARM汇编
  11. idea输入法不跟随解决办法
  12. k8s 1.9 安装
  13. 在VS2010中使用Git
  14. LeetCode--292--Nim游戏
  15. GIT学习---GIT&amp;github的使用
  16. asp.net DropDownList实现二级联动效果
  17. logisitic回归
  18. 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)
  19. Python基础之socket编程(Day29)
  20. Mac下配置mnmp环境

热门文章

  1. unity3d easytouch计算摇杆旋转角度以及摇杆八方向控制角色
  2. 步骤2:JMeter 分布式测试(性能测试大并发、远程启动解决方案)
  3. 第九篇 Python数据类型之集合
  4. CSP201409-1:相邻数对
  5. 洛谷P2346四子连棋
  6. LeetCode 86 ——分隔链表
  7. nodeJs 调试异步程序追踪异步报错
  8. NO5——素数筛选
  9. 【UML】活动图介绍
  10. Log4Net讲解