标签:des style blog http color os io 数据
对于30%的数据,N ≤ 20,M ≤ 120。
对于100%的数据,N ≤ 200,M ≤ 20000。
1 const inf=maxlongint; 2 type node=record 3 from,go,next,v,c:longint; 4 end; 5 var e:array[0..2000000] of node; 6 pre,head,q,d:array[0..1000000] of longint; 7 v:array[0..1000000] of boolean; 8 i,j,n,m,s,t,l,r,mincost,tot,x,y,z,maxflow:longint; 9 function min(x,y:longint):longint; 10 begin 11 if x<y then exit(x) else exit(y); 12 end; 13 procedure ins(x,y,z,w:longint); 14 begin 15 inc(tot); 16 with e[tot] do 17 begin 18 from:=x;go:=y;v:=z;c:=w;next:=head[x];head[x]:=tot; 19 end; 20 end; 21 procedure insert(x,y,z,w:longint); 22 begin 23 ins(x,y,z,w);ins(y,x,0,-w); 24 end; 25 function spfa:boolean; 26 var i,x,y:longint; 27 begin 28 fillchar(v,sizeof(v),false); 29 for i:=s to t do d[i]:=inf; 30 l:=0;r:=1;q[1]:=s;d[s]:=0;v[s]:=true; 31 while l<r do 32 begin 33 inc(l);if l=1000000 then l:=0; 34 x:=q[l];v[x]:=false; 35 i:=head[x]; 36 while i<>0 do 37 begin 38 y:=e[i].go; 39 if (e[i].v<>0) and (d[x]+e[i].c<d[y]) then 40 begin 41 d[y]:=d[x]+e[i].c; 42 pre[y]:=i; 43 if not(v[y]) then 44 begin 45 v[y]:=true; 46 inc(r);if r=1000000 then r:=0; 47 q[r]:=y; 48 end; 49 end; 50 i:=e[i].next; 51 end; 52 end; 53 exit(d[t]<>inf); 54 end; 55 procedure mcf; 56 var i,tmp:longint; 57 begin 58 mincost:=0;maxflow:=0; 59 while spfa do 60 begin 61 tmp:=inf; 62 i:=pre[t]; 63 while i<>0 do 64 begin 65 tmp:=min(tmp,e[i].v); 66 i:=pre[e[i].from]; 67 end; 68 inc(mincost,tmp*d[t]); 69 inc(maxflow,tmp); 70 i:=pre[t]; 71 while i<>0 do 72 begin 73 dec(e[i].v,tmp); 74 inc(e[i xor 1].v,tmp); 75 i:=pre[e[i].from]; 76 end; 77 end; 78 end; 79 procedure init; 80 begin 81 tot:=1; 82 readln(n,m); 83 for i:=1 to m do 84 begin 85 readln(x,y,z); 86 insert(x+n,y,1,z); 87 end; 88 for i:=2 to n-1 do insert(i,i+n,1,0); 89 s:=1;t:=2*n; 90 insert(s,s+n,inf,0);insert(n,t,inf,0); 91 end; 92 procedure main; 93 begin 94 mincost:=0; 95 mcf; 96 writeln(maxflow,‘ ‘,mincost); 97 end; 98 begin 99 assign(input,‘input.txt‘);assign(output,‘output.txt‘); 100 reset(input);rewrite(output); 101 init; 102 main; 103 close(input);close(output); 104 end.
BZOJ1877: [SDOI2009]晨跑,布布扣,bubuko.com
标签:des style blog http color os io 数据
原文地址:http://www.cnblogs.com/zyfzyf/p/3901418.html