网络流/poj1273 Drainage Ditches

题意

  一个有向图,给出n个点,m条边,给出每条边的起点和中点以及容量,求最大流

分析

  求最大流模板

Accepted Code

dinic

 1 {
 2     PROBLEM:poj 1273
 3     AUTHER:Rinyo
 4     MEMO:网络流   
 5 }
 6 Program flow;
 7 Const
 8   Infile = 'flow.out';
 9   Outfile = 'flow.out';
10 Var       
11   q:Array[0..100000]Of Longint;
12   map:Array[0..400,0..400]Of Longint;
13   num:Array[0..400]Of Longint;
14   sum,i,n,m,x,y,w:Longint;
15   flag:Boolean;
16 
17 Function min(a,b:Longint):Longint;
18 Begin
19   If a<b Then min:=a Else min:=b;
20 End;
21 
22 Function dfs(now,value:Longint):Longint;
23 Var
24   v1,x,i:Longint;
25 Begin
26   If (now=n) Then Begin
27     dfs:=value;
28     Exit;
29   End;
30   v1:=value;
31   For i:=1 To n Do
32     If (map[now,i]>0) And (num[now]+1=num[i]) Then Begin
33       x:=dfs(i,min(value,map[now,i]));
34       value:=value-x;
35       Dec(map[now,i],x);
36       Inc(map[i,now],x);
37     End;
38   dfs:=v1-value;
39 End;
40 
41 Function bfs:Boolean;
42 Var
43   head,tail,now,i:Longint;
44 Begin
45   Fillchar(q,sizeof(q),0);
46   q[1]:=1;head:=0;tail:=1;flag:=false;
47   Fillchar(num,sizeof(num),0);
48   num[1]:=1;
49   While head<tail Do Begin
50     Inc(head);
51     now:=q[head];
52     For i:=1 To n Do
53       If map[now,i]>0 Then
54       If num[i]=0 Then Begin
55         Inc(tail);
56         q[tail]:=i;
57         num[i]:=num[now]+1;
58         If i=n Then begin
59           flag:=true;
60           Exit;
61         End;
62       End;
63   End;
64 End;
65 
66 Begin
67   Assign(input,infile);Reset(input);
68   Assign(output,outfile);Rewrite(output);
69   While not eof Do Begin
70     ReadLn(m,n);
71     Fillchar(map,sizeof(map),0);
72     For i:=1 To m Do begin
73       ReadLn(x,y,w);
74       map[x,y]:=map[x,y]+w;
75     End;
76     flag:=true;
77     sum:=0;
78     While flag Do Begin
79       bfs;
80       sum:=sum+dfs(1,maxlongint);
81     End;
82     WriteLn(sum);
83   End;
84   Close(input);Close(output);
85 End.

sap

  1 {
  2     PROBLEM:poj 1273
  3     AUTHER:Rinyo
  4     MEMO:网络流   
  5 }
  6 
  7 Program flow;
  8 Const
  9   Infile = 'flow.in';
 10   Outfile = 'flow.out';
 11 Var      
 12   maxflow,n,m,i,x,y,z:Longint;
 13   map,temp:Array[0..430,0..430]Of Longint;
 14   num,q,pre:Array[0..10000]Of Longint;
 15 
 16 Function min(a,b:Longint):Longint;
 17 Begin
 18   if a<b Then Exit(a) Else Exit(b);
 19 End;
 20 
 21 Procedure bfs;
 22 Var
 23   head,tail,i,now:Longint;
 24 Begin
 25   Fillchar(q,sizeof(q),0);
 26   q[1]:=n;head:=0;tail:=1;num[n]:=0;
 27   Fillchar(num,sizeof(num),0);
 28   While head<tail Do Begin
 29     Inc(head);
 30     now:=q[head];
 31     For i:=1 To n Do
 32       if (temp[now,i]>0) And (num[i]=0) Then Begin
 33         Inc(tail);
 34         q[tail]:=i;
 35         num[i]:=num[now]+1;
 36         if i=n Then Exit;
 37       End;
 38   End;
 39 End;
 40 
 41 Function findallowed(i:Longint):Longint;
 42 Var
 43   j:Longint;
 44 Begin
 45   For j:=1 To n Do
 46     If (map[i,j]>0) And (num[i]=num[j]+1) Then Exit(j);
 47   Exit(-1);
 48 End;
 49 
 50 Function relable(i:Longint):Longint;
 51 Var
 52   t,j:Longint;
 53 Begin
 54   t:=maxlongint;
 55   For j:=1 To n Do
 56     if map[i,j]>0 Then t:=min(t,num[j]+1);
 57   If t=maxlongint Then Exit(n) Else Exit(t);
 58 End;
 59 
 60 Function sap:Longint;
 61 Var
 62   i,j,ll,x:Longint;
 63 Begin
 64   maxflow:=0;i:=1;
 65   While (num[1]<n) Do Begin
 66     j:=findallowed(i);
 67     If (j>=0) Then Begin
 68       pre[j]:=i;i:=j;
 69       If (i=n) Then Begin
 70         ll:=maxlongint;
 71         i:=n;
 72         While i<>1 Do BEgin
 73           ll:=min(ll,map[pre[i],i]);
 74           i:=pre[i];
 75         End;
 76         i:=n;
 77         While i<>1 Do Begin
 78           map[pre[i],i]:=map[pre[i],i]-ll;
 79           map[i,pre[i]]:=map[i,pre[i]]+ll;
 80           i:=pre[i];
 81         End;
 82         maxflow:=maxflow+ll;
 83       End;
 84     End Else Begin
 85       x:=relable(i);
 86       num[i]:=x;
 87       While i<>1 Do i:=pre[i];
 88     End;
 89   End;
 90 End;
 91 
 92 Begin
 93   Assign(input,infile);Reset(input);
 94   Assign(output,outfile);Rewrite(output);
 95   While not eof Do begin
 96     Fillchar(map,sizeof(map),0);
 97     Fillchar(temp,sizeof(temp),0);
 98     Fillchar(pre,sizeof(pre),$ff);
 99     Fillchar(num,sizeof(num),0);
100     ReadLn(m,n);
101     For i:=1 To m Do Begin
102       ReadLn(x,y,z);
103       map[x,y]:=map[x,y]+z;
104       temp[y,x]:=temp[y,x]+z;
105     End;
106     bfs;
107     sap;
108     WriteLn(maxflow);
109   End;
110   Close(input);Close(output);
111 End.

 

posted @ 2013-03-24 21:34  Rinyo  阅读(214)  评论(0编辑  收藏  举报