1834: [ZJOI2010]network 网络扩容 (最小费用最大流模板)

 1 const inf=10000000;
 2 var
 3   other,a,dis,pre,uu,vv,ww,next,head,e,x,long,w:array[0..50100]of longint;
 4   v:Array[0..10010]of boolean;
 5   tt,c,ee,s,t,i,j,ans,mincost,n,m,k:longint;
 6 function min(aa,bb:longint):longint;
 7   begin
 8   if aa>bb then exit(bb)
 9   else exit(aa);
10   end;
11  
12 function spfa:boolean;
13   var i,h,t,j:longint;
14   begin
15   h:=1;t:=1;
16   for i:=0 to tt do dis[i]:=inf;
17   a[1]:=s; pre[s]:=-1;
18   v[1]:=true;  dis[s]:=0;
19   while h<=t do
20     begin
21     j:=head[a[h]];
22     while j<>0 do
23       begin
24       if (long[j]>0)and(dis[a[h]]+w[j]<dis[e[j]]) then
25         begin
26         dis[e[j]]:=dis[a[h]]+w[j];
27         pre[e[j]]:=j;
28         if not v[e[j]] then
29           begin
30           v[e[j]]:=true;
31           inc(t);a[t]:=e[j];
32           end;
33         end;
34       j:=next[j];
35       end;
36     v[a[h]]:=false;
37     inc(h);
38     end;
39   if dis[tt]>=inf-1000 then exit(false)
40   else exit(true);
41   end;
42  
43 procedure work;
44   var zh,flow:longint;
45   begin
46   ans:=0;
47   mincost:=0;
48   while spfa do
49     begin
50     zh:=tt;
51     flow:=maxlongint;
52     while pre[zh]<>-1 do
53       begin
54       flow:=min(flow,long[pre[zh]]);
55       zh:=x[pre[zh]];
56       end;
57     ans:=ans+flow;
58     mincost:=mincost+dis[tt]*flow;
59     zh:=tt;
60     while pre[zh]<>-1 do
61       begin
62       dec(long[pre[zh]],flow);
63       inc(long[other[pre[zh]]],flow);
64       zh:=x[pre[zh]];
65       end;
66     end;
67   end;
68  
69 procedure add(u,v,c,cc:longint);
70   begin
71   inc(ee);x[ee]:=u;e[ee]:=v;long[ee]:=c;w[ee]:=cc;other[ee]:=ee+1;
72   next[ee]:=head[u];head[u]:=ee;
73   inc(ee);x[ee]:=v;e[ee]:=u;long[ee]:=0;w[ee]:=-cc;other[ee]:=ee-1;
74   next[ee]:=head[v];head[v]:=ee;
75   end;
76  
77 begin
78   readln(n,m,k);
79   for i:=1 to m do
80     begin
81     readln(uu[i],vv[i],c,ww[i]);
82     add(uu[i],vv[i],c,0);
83     end;
84   s:=1;tt:=n;
85   work;
86   write(ans,' ');
87   tt:=n+1;
88   add(n,n+1,k,0);
89   for i:=1 to m do
90     add(uu[i],vv[i],inf,ww[i]);
91   work;
92   writeln(mincost);
93   end.
------------------------------------------------------------------------- 花有重开日,人无再少年
原文地址:https://www.cnblogs.com/lbz007oi/p/2948622.html