poj 1273 最大流入门

明天再拍一遍

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 const int N = 210;
 5 const int INF = 0x7FFFFFFF;
 6 int n,m,map[N][N],path[N],flow[N],start,end;
 7 queue<int> q;
 8 int bfs()
 9 {
10     int i,t;
11     while(!q.empty()) q.pop();
12     memset(path,-1,sizeof(path));
13     path[start]=0,flow[start]=INF;
14     q.push(start);
15     while(!q.empty())
16     {
17         t=q.front();
18         q.pop();
19         if(t==end) break;
20         for(i=1;i<=m;i++)
21         {
22             if(i!=start && path[i]==-1 && map[t][i])
23             {
24                 flow[i]=flow[t]<map[t][i]?flow[t]:map[t][i];
25                 q.push(i);
26                 path[i]=t;
27             }
28         }
29     }
30     if(path[end]==-1) return -1;
31     return flow[m];                   //一次遍历之后的流量增量
32 }
33 int Edmonds_Karp()
34 {
35     int max_flow=0,step,now,pre;
36     while((step=bfs())!=-1)
37     {          //找不到增路径时退出
38         max_flow+=step;
39         now=end;
40         while(now!=start)
41         {
42             pre=path[now];
43             map[pre][now]-=step;      //更新正向边的实际容量
44             map[now][pre]+=step;      //添加反向边
45             now=pre;
46         }
47     }
48     return max_flow;
49 }
50  int main()
51  {
52     int i,u,v,cost;
53     while(scanf("%d %d",&n,&m)!=EOF)
54     {
55         memset(map,0,sizeof(map));
56         for(i=0;i<n;i++)
57         {
58             scanf("%d %d %d",&u,&v,&cost);
59             map[u][v]+=cost;           //not just only one input
60         }
61         start=1,end=m;
62         printf("%d
",Edmonds_Karp());
63     }
64     return 0;
65 }
原文地址:https://www.cnblogs.com/cnblogs321114287/p/4278386.html