hdu1532 (最大流入门,EK算法)

看着这个博客 

然后敲了hdu1532这个入门题,算是对最大流有点理解了

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 using namespace std;
 5 const int INF = 1<<30;
 6 const int N = 200 + 10;
 7 int n,m;
 8 int cap[N][N],flow[N],pre[N];
 9 int bfs(int src, int des)//每次用bfs找到最小的delta
10 {
11     queue<int> q;
12     for(int i=1;i<=n; ++i)
13         pre[i] = -1;
14     pre[src] = 0;
15     flow[src] = INF;
16     q.push(src);
17     while(!q.empty())
18     {
19         int u = q.front(); q.pop();
20     
21         for(int i=1;i<=n;++i)  
22         if(i!=src && cap[u][i]>0 && pre[i]==-1)
23         {
24             pre[i] = u;
25             flow[i] = min(cap[u][i],flow[u]);
26             q.push(i);
27         }
28     }
29     //printf("%d\n",pre[des]);
30     if(pre[des]==-1)
31         return -1;
32     else
33         return flow[des];
34 }
35 int maxFlow(int src, int des)
36 {
37     int sumFlow = 0;
38     int add = 0;
39     while((add = bfs(src,des)) != -1)//如果不为-1,那么就进行增广
40     {
41         int k = des;
42         while(k!=src)
43         {
44             int fa = pre[k];
45             cap[fa][k] -= add;
46             cap[k][fa] += add;
47             k = fa;
48         }
49         sumFlow += add;
50     }
51     return sumFlow;
52     
53 }
54 int main()
55 {
56     
57     while(scanf("%d%d",&m,&n)!=EOF)
58     {
59         int u,v,c;
60         memset(cap,0,sizeof(cap));
61         for(int i=1;i<=m;++i)
62         {
63             scanf("%d%d%d",&u,&v,&c);
64             cap[u][v] += c;
65 
66         }
67         int ans = maxFlow(1,n);
68         printf("%d\n",ans);
69     }
70     return 0;
71 }

 

你可能感兴趣的:(hdu1532 (最大流入门,EK算法))