bzoj 1061 志愿者招募 费用流
2024-10-17 23:43:24
详见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.
最新文章
- MVC 外网 上传 下载 实现方式(一)
- app启动速度
- spring3.0事务的配置
- GitHub学习笔记
- IAsyncResult 接口异步 和匿名委托
- 班级博客客户端Beta阶段发布说明
- javaweb学习总结(四)——Http协议(转)
- 工厂模式讲解, 引入Spring IOC
- jmeter和jdk的安装教程
- iOS相关的ARM汇编
- idea输入法不跟随解决办法
- k8s 1.9 安装
- 在VS2010中使用Git
- LeetCode--292--Nim游戏
- GIT学习---GIT&;github的使用
- asp.net DropDownList实现二级联动效果
- logisitic回归
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)
- Python基础之socket编程(Day29)
- Mac下配置mnmp环境