标签:
1 const maxn=1000; maxe=2000;
2 type
3 node=record
4 f,t,l,re:longint;
5 end;
6 var m,n,i,j,u,v,x,f,t,num:longint;
7 e,h,head:array[0..maxn] of longint;
8 b:array[0..maxe] of node;
9 function min(a,b:longint):longint;
10 begin
11 if a>b then exit(b)
12 else exit(a);
13 end;
14 procedure insert(u,v,x,rev:longint);
15 begin
16 inc(num);
17 with b[num] do
18 begin
19 f:=head[u];
20 t:=v;
21 l:=x;
22 re:=rev;
23 end;
24 head[u]:=num;
25 end;
26 function findnode:longint;
27 var i:longint;
28 begin
29 for i:=2 to n-1 do
30 if e[i]>0 then exit(i);
31 exit(0);
32 end;
33 function push(u:longint):boolean;
34 var nowe,flow,v:longint;
35 begin
36 nowe:=head[u]; push:=true;
37 while nowe<>0 do
38 begin
39 v:=b[nowe].t;
40 if (h[v]+1=h[u]) and (b[nowe].l>0) then
41 begin
42 flow:=min(e[u],b[nowe].l);
43 dec(e[u],flow);
44 dec(b[nowe].l,flow);
45 inc(e[v],flow);
46 inc(b[b[nowe].re].l,flow);
47 push:=false;
48 end;
49 nowe:=b[nowe].f;
50 end;
51 end;
52 procedure relable(u:longint);
53 var minh,v,nowe:longint;
54 begin
55 minh:=maxn;
56 nowe:=head[u];
57 while nowe<>0 do
58 begin
59 v:=b[nowe].t;
60 if b[nowe].l>0 then minh:=min(minh,h[v]);
61 nowe:=b[nowe].f;
62 end;
63 h[u]:=minh+1;
64 end;
65 begin
66 readln(m,n); f:=1; t:=n;
67 for i:=1 to m do
68 begin
69 readln(u,v,x);
70 if u=v then continue;
71 insert(u,v,x,num+2);
72 insert(v,u,0,num);
73 end;
74 u:=head[f];
75 while u<>0 do
76 begin
77 v:=b[u].t; x:=b[u].l;
78 inc(e[v],x);
79 dec(e[f],x);
80 b[b[u].re].l:=x;
81 b[u].l:=0;
82 u:=b[u].f;
83 end;
84 h[f]:=n+1;
85 u:=findnode;
86 while u<>0 do
87 begin
88 if push(u) then relable(u);
89 u:=findnode;
90 end;
91 writeln(e[n]);
92 end.
预流推进
1 const inf=1000000000; maxe=1000000; maxn=10000;
2 type
3 node=record
4 f,t,c,d:longint;
5 end;
6 var n,a,b,fbuy,fa,fb,i,s,t,x,num,ans,temp:longint;
7 e:array[0..maxe] of node;
8 head,go,d:array[0..maxn] of longint;
9 f:array[0..maxe] of longint;
10 p:array[0..maxn] of boolean;
11 procedure insert(u,v,x,y:longint);
12 begin
13 e[num].f:=head[u];
14 e[num].t:=v;
15 e[num].c:=x;
16 e[num].d:=y;
17 head[u]:=num;
18 inc(num);
19 end;
20 procedure add(u,v,x,y:longint);
21 begin
22 insert(u,v,x,y);
23 insert(v,u,0,-y);
24 end;
25 function spfa:boolean;
26 var l,r,edge,now,v:longint;
27 begin
28 fillchar(p,sizeof(p),true);
29 fillchar(go,sizeof(go),255);
30 for l:=1 to t do d[l]:=inf;
31 l:=1; r:=1; f[1]:=s; d[s]:=0; p[s]:=false; temp:=0;
32 while l<=r do
33 begin
34 now:=f[l];
35 edge:=head[now];
36 while edge<>-1 do
37 begin
38 v:=e[edge].t;
39 if (d[v]>d[now]+e[edge].d) and (e[edge].c>0) then
40 begin
41 d[v]:=d[now]+e[edge].d;
42 go[v]:=edge;
43 if p[v] then
44 begin
45 p[v]:=false;
46 inc(r);
47 f[r]:=v;
48 end;
49 end;
50 edge:=e[edge].f;
51 end;
52 inc(l);
53 p[now]:=true;
54 end;
55 if d[t]=inf then exit(false)
56 else begin
57 temp:=d[t];
58 exit(true);
59 end;
60 end;
61 function min(a,b:longint):longint;
62 begin if a>b then exit(b) else exit(a); end;
63 function calc:longint;
64 var now:longint;
65 begin
66 now:=t; calc:=inf;
67 while go[now]<>-1 do
68 begin
69 calc:=min(calc,e[go[now]].c);
70 now:=e[go[now] xor 1].t;
71 end;
72 now:=t;
73 while go[now]<>-1 do
74 begin
75 dec(e[go[now]].c,calc);
76 inc(e[go[now] xor 1].c,calc);
77 now:=e[go[now] xor 1].t;
78 end;
79 end;
80 begin
81 readln(n,a,b,fbuy,fa,fb);
82 fillchar(e,sizeof(e),255);
83 fillchar(head,sizeof(head),255);
84 s:=2*n+1; t:=s+1;
85 for i:=1 to n do
86 begin
87 read(x);
88 if i+1<=n then add(i,i+1,inf,0);
89 if i+a+1<=n then add(i,n+i+a+1,inf,fa);
90 if i+b+1<=n then add(i,n+i+b+1,inf,fb);
91 add(s,n+i,inf,fbuy);
92 add(s,i,x,0);
93 add(n+i,t,x,0);
94 end;
95 while spfa do
96 begin
97 //writeln(temp);
98 inc(ans,temp*calc);
99 end;
100 writeln(ans);
101 end.
EK 的最小费用
(转载请注明出处:http://www.cnblogs.com/Kalenda/)
标签:
原文地址:http://www.cnblogs.com/Kalenda/p/4862169.html