博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bzoj2424
阅读量:7171 次
发布时间:2019-06-29

本文共 2508 字,大约阅读时间需要 8 分钟。

比较简单的费用流,一目了然

1 const inf=10000007;  2 type node=record  3        next,point,flow,cost:longint;  4      end;  5   6 var edge:array[0..200010] of node;  7     q:array[0..200010] of longint;  8     p,d,pre,cur,u,w:array[0..200] of longint;  9     v:array[0..200] of boolean; 10     s,i,len,n,m,t:longint; 11  12 function min(a,b:longint):longint; 13   begin 14     if a>b then exit(b) else exit(a); 15   end; 16  17 procedure add(x,y,f,c:longint); 18   begin 19     inc(len); 20     edge[len].point:=y; 21     edge[len].flow:=f; 22     edge[len].cost:=c; 23     edge[len].next:=p[x]; 24     p[x]:=len; 25   end; 26  27 function spfa:boolean; 28   var y,i,f,r,x:longint; 29   begin 30     d[0]:=0; 31     for i:=1 to t do 32       d[i]:=inf; 33     f:=1; 34     r:=1; 35     q[1]:=0; 36     fillchar(v,sizeof(v),false); 37     v[0]:=true; 38     while f<=r do 39     begin 40       x:=q[f]; 41       v[x]:=false; 42       i:=p[x]; 43       while i<>-1 do 44       begin 45         y:=edge[i].point; 46         if edge[i].flow>0 then 47           if d[y]>d[x]+edge[i].cost then 48           begin 49             d[y]:=d[x]+edge[i].cost; 50             pre[y]:=x; 51             cur[y]:=i; 52             if not v[y] then 53             begin 54               inc(r); 55               q[r]:=y; 56               v[y]:=true; 57             end; 58           end; 59         i:=edge[i].next; 60       end; 61       inc(f); 62     end; 63     if d[t]=inf then exit(false) else exit(true); 64   end; 65  66 function mincost:longint; 67   var neck,i,j:longint; 68   begin 69     mincost:=0; 70     while spfa do 71     begin 72       neck:=inf; 73       i:=t; 74       while i<>0 do 75       begin 76         j:=cur[i]; 77         neck:=min(neck,edge[j].flow); 78         i:=pre[i]; 79       end; 80       i:=t; 81       while i<>0 do 82       begin 83         j:=cur[i]; 84         dec(edge[j].flow,neck); 85         inc(edge[j xor 1].flow,neck); 86         i:=pre[i]; 87       end; 88       mincost:=mincost+d[t]*neck; 89     end; 90   end; 91  92 begin 93   len:=-1; 94   fillchar(p,sizeof(p),255); 95   readln(n,m,s); 96   t:=n+1; 97   for i:=1 to n do 98     read(u[i]); 99   for i:=1 to n do100   begin101     read(w[i]);102     add(0,i,inf,w[i]);103     add(i,0,0,-w[i]);104     add(i,t,u[i],0);105     add(t,i,0,0);106     if i<>n then107     begin108       add(i,i+1,s,m);109       add(i+1,i,0,-m);110     end;111   end;112   writeln(mincost);113 end.
View Code

 

转载于:https://www.cnblogs.com/phile/p/4473072.html

你可能感兴趣的文章
HTML学习
查看>>
Warriors of the Visual Studio, Assemble! (Visual Studio的勇士们,汇编吧!)
查看>>
使用Aouth2进行身份验证
查看>>
我们有助教啦
查看>>
一个有关原型的问题牵扯出的问题
查看>>
P53 T3
查看>>
关于 tensorflow-gpu 中 CUDA 和 CuDNN 版本适配问题
查看>>
1、JUC--volatile 关键字-内存可见性
查看>>
LeetCode: Minimum Depth of Binary Tree
查看>>
可运行的代码
查看>>
Oracle数据库添加新字段后加载页面报错 java.lang.IllegalArgumentException
查看>>
CSU 1505: 酷酷的单词【字符串】
查看>>
198. 打家劫舍
查看>>
错误之处(二)
查看>>
解决insert语句插入时,需要写列值的问题
查看>>
CSS选择器 < ~ +
查看>>
Opengl_es模型矩阵位置:glFrustumx与glTranslatef参数的相互影响--立方体旋转特效
查看>>
JS小功能系列8省市联动
查看>>
《程序是怎样跑起来的》第八章读后感
查看>>
YCD 软件更新方法
查看>>