标签:proc points exp nts eset ram next with any
Description
Input
Output
Sample Input
3 4
1 1
1 3
2 2
3 2
Sample Output
2
Hint
Source
1 program rrr(input,output); 2 const 3 inf=123456789; 4 type 5 etype=record 6 t,c,rev,next:longint; 7 end; 8 var 9 e:array[0..20020]of etype; 10 a,cur,d:array[-505..505]of longint; 11 q:array[0..1010]of longint; 12 n,m,i,x,y,h,t,cnt,ans:longint; 13 procedure ins(x,y,c:longint); 14 begin 15 inc(cnt);e[cnt].t:=y;e[cnt].c:=c;e[cnt].next:=a[x];a[x]:=cnt; 16 end; 17 procedure add(x,y:longint); 18 begin 19 ins(x,y,1);ins(y,x,0); 20 e[cnt].rev:=cnt-1;e[cnt-1].rev:=cnt; 21 end; 22 function min(a,b:longint):longint; 23 begin 24 if a<b then exit(a) else exit(b); 25 end; 26 procedure bfs; 27 begin 28 for i:=-n to n+1 do d[i]:=-1; 29 h:=0;t:=1;q[1]:=0;d[0]:=0; 30 while h<t do 31 begin 32 inc(h); 33 i:=a[q[h]]; 34 while i<>inf do 35 begin 36 if (d[e[i].t]=-1) and (e[i].c>0) then 37 begin 38 d[e[i].t]:=d[q[h]]+1; 39 inc(t);q[t]:=e[i].t; 40 end; 41 i:=e[i].next; 42 end; 43 end; 44 end; 45 function dfs(k,f:longint):longint; 46 var 47 ans,r,i:longint; 48 begin 49 if (k=n+1) or (f=0) then exit(f); 50 ans:=0;i:=cur[k]; 51 while i<>inf do 52 begin 53 if (e[i].c>0) and (d[e[i].t]=d[k]+1) then 54 begin 55 r:=dfs(e[i].t,min(f,e[i].c)); 56 dec(e[i].c,r);inc(e[e[i].rev].c,r); 57 ans:=ans+r;f:=f-r; 58 if f=0 then break; 59 end; 60 i:=e[i].next; 61 cur[k]:=i; 62 end; 63 if f>0 then d[k]:=-1; 64 exit(ans); 65 end; 66 begin 67 assign(input,‘r.in‘);assign(output,‘r.out‘);reset(input);rewrite(output); 68 readln(n,m); 69 cnt:=0;for i:=-n to n+1 do a[i]:=inf; 70 for i:=1 to n do begin add(0,-i);add(i,n+1); end; 71 for i:=1 to m do begin readln(x,y);add(-x,y); end; 72 ans:=0; 73 while true do 74 begin 75 bfs; 76 if d[n+1]=-1 then break; 77 for i:=-n to n+1 do cur[i]:=a[i]; 78 ans:=ans+dfs(0,inf); 79 end; 80 write(ans); 81 close(input);close(output); 82 end.
poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)
标签:proc points exp nts eset ram next with any
原文地址:http://www.cnblogs.com/Currier/p/6535734.html