标签:
在第一个例子中,所有小朋友都投赞成票就能得到最优解
1 /************************************************************** 2 Problem: 1934 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:60 ms 7 Memory:3484 kb 8 ****************************************************************/ 9 10 type 11 point=^node; 12 node=record 13 g,w:longint; 14 next,anti:point; 15 end; 16 var 17 i,j,k,l,m,n,ans,s,t:longint; 18 a:array[0..100000] of point; 19 c,d,dv:array[0..100000] of longint; 20 function min(x,y:longint):longint;inline; 21 begin 22 if x<y then min:=x else min:=y; 23 end; 24 procedure add(x,y,z:longint);inline; 25 var p:point; 26 begin 27 new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p; 28 new(p);p^.g:=x;p^.w:=0;p^.next:=a[y];a[y]:=p; 29 a[x]^.anti:=a[y];a[y]^.anti:=a[x]; 30 end; 31 function dfs(x,flow:longint):longint;inline; 32 var i,j,k,l:longint;p:point; 33 begin 34 if x=t then exit(flow); 35 dfs:=0;p:=a[x]; 36 while p<>nil do 37 begin 38 if (p^.w>0) and (d[x]=(d[p^.g]+1)) then 39 begin 40 k:=dfs(p^.g,min(flow-dfs,p^.w)); 41 dec(p^.w,k); 42 inc(p^.anti^.w,k); 43 inc(dfs,k); 44 if dfs=flow then exit; 45 end; 46 p:=p^.next; 47 end; 48 if d[s]=n then exit; 49 dec(dv[d[x]]); 50 if dv[d[x]]=0 then d[s]:=n; 51 inc(d[x]); 52 inc(dv[d[x]]); 53 end; 54 begin 55 readln(n,m);s:=1;t:=n+2; 56 for i:=1 to n+2 do a[i]:=nil; 57 for i:=1 to n do 58 begin 59 read(c[i]); 60 if c[i]=1 then add(1,i+1,1) else add(i+1,n+2,1); 61 end; 62 readln; 63 for i:=1 to m do 64 begin 65 readln(j,k); 66 if c[j]=1 then add(j+1,k+1,1) else add(k+1,j+1,1); 67 end; 68 n:=n+2; 69 ans:=0; 70 fillchar(d,sizeof(d),0); 71 fillchar(dv,sizeof(dv),0);dv[0]:=n; 72 while d[s]<n do inc(ans,dfs(s,maxlongint)); 73 writeln(ans); 74 readln; 75 end.
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4294398.html