标签:des style blog http color os io 文件
在第一个例子中,所有小朋友都投赞成票就能得到最优解
1 const inf=maxlongint; 2 type node=record 3 from,go,next,v:longint; 4 end; 5 var tot,i,j,n,m,maxflow,l,r,s,t,x,y:longint; 6 h,head,q,cur:array[0..1000] of longint; 7 e:array[0..200000] of node; 8 function min(x,y:longint):longint; 9 begin 10 if x<y then exit(x) else exit(y); 11 end; 12 procedure ins(x,y,z:longint); 13 begin 14 inc(tot); 15 e[tot].from:=x;e[tot].go:=y;e[tot].v:=z;e[tot].next:=head[x];head[x]:=tot; 16 end; 17 procedure insert(x,y,z:longint); 18 begin 19 ins(x,y,z);ins(y,x,0); 20 end; 21 function bfs:boolean; 22 var i,x,y:longint; 23 begin 24 fillchar(h,sizeof(h),0); 25 l:=0;r:=1;q[1]:=s;h[s]:=1; 26 while l<r do 27 begin 28 inc(l); 29 x:=q[l]; 30 i:=head[x]; 31 while i<>0 do 32 begin 33 y:=e[i].go; 34 if (e[i].v<>0) and (h[y]=0) then 35 begin 36 h[y]:=h[x]+1; 37 inc(r);q[r]:=y; 38 end; 39 i:=e[i].next; 40 end; 41 end; 42 exit (h[t]<>0); 43 end; 44 function dfs(x,f:longint):longint; 45 var i,y,used,tmp:longint; 46 begin 47 if x=t then exit(f); 48 used:=0; 49 i:=cur[x]; 50 while i<>0 do 51 begin 52 y:=e[i].go; 53 if (h[y]=h[x]+1) and (e[i].v<>0) then 54 begin 55 tmp:=dfs(y,min(e[i].v,f-used)); 56 dec(e[i].v,tmp);if e[i].v<>0 then cur[x]:=i; 57 inc(e[i xor 1].v,tmp); 58 inc(used,tmp); 59 if used=f then exit(f); 60 end; 61 i:=e[i].next; 62 end; 63 if used=0 then h[x]:=-1; 64 exit(used); 65 end; 66 procedure dinic; 67 begin 68 while bfs do 69 begin 70 for i:=s to t do cur[i]:=head[i]; 71 inc(maxflow,dfs(s,inf)); 72 end; 73 end; 74 procedure init; 75 begin 76 tot:=1; 77 readln(n,m); 78 s:=0;t:=n+1; 79 for i:=1 to n do 80 begin 81 read(x); 82 if x=0 then insert(s,i,1) 83 else insert(i,t,1); 84 end; 85 readln; 86 for i:=1 to m do begin readln(x,y);insert(x,y,1);insert(y,x,1);end; 87 end; 88 procedure main; 89 begin 90 maxflow:=0; 91 dinic; 92 writeln(maxflow); 93 end; 94 begin 95 assign(input,‘input.txt‘);assign(output,‘output.txt‘); 96 reset(input);rewrite(output); 97 init; 98 main; 99 close(input);close(output); 100 end.
BZOJ1934: [Shoi2007]Vote 善意的投票,布布扣,bubuko.com
BZOJ1934: [Shoi2007]Vote 善意的投票
标签:des style blog http color os io 文件
原文地址:http://www.cnblogs.com/zyfzyf/p/3908343.html