hdu 1532 Drainage Ditches 【ISAP 】

还是不是很懂算法

先存一个模板先吧~~~

看的这篇学的--

http://www.renfei.org/blog/isap.html

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<vector>
  6 #include<queue>
  7 using namespace std;
  8 
  9 const int maxn = 1005;
 10 const int INF = (1 << 30) - 1;
 11 
 12 struct Edge{
 13     int from,to,cap,flow;
 14 };
 15 
 16 vector<Edge> edges;
 17 
 18 int source;         // 源点
 19 int sink;           // 汇点
 20 int p[maxn];   // 可增广路上的上一条弧的编号
 21 int num[maxn]; // 和 t 的最短距离等于 i 的节点数量
 22 int cur[maxn]; // 当前弧下标
 23 int d[maxn];   // 残量网络中节点 i 到汇点 t 的最短距离
 24 bool visited[maxn];
 25 
 26 int num_nodes;
 27 
 28 vector<int> G[maxn];
 29 
 30 // 预处理, 反向 BFS 构造 d 数组
 31 bool bfs()
 32 {
 33     memset(visited, 0, sizeof(visited));
 34     queue<int> Q;
 35     Q.push(sink);
 36     visited[sink] = 1;
 37     d[sink] = 0;
 38     while (!Q.empty()) {
 39         int u = Q.front();
 40         Q.pop();
 41         for ( int i = 0;i < G[u].size();i++) {
 42             Edge &e = edges[G[u][i]^1];
 43             if (!visited[e.from] && e.cap> e.flow) {
 44                 visited[e.from] = true;
 45                 d[e.from] = d[u] + 1;
 46                 Q.push(e.from);
 47             }
 48         }
 49     }
 50     return visited[source];
 51 }
 52 
 53 // 增广
 54 int augment()
 55 {
 56     int u = sink, df = INF;
 57     // 从汇点到源点通过 p 追踪增广路径, df 为一路上最小的残量
 58     while (u != source) {
 59         Edge &e = edges[p[u]];
 60         df = min(df, e.cap - e.flow);
 61         u = edges[p[u]].from;
 62     }
 63     u = sink;
 64     // 从汇点到源点更新流量
 65     while (u != source) {
 66         edges[p[u]].flow += df;
 67         edges[p[u]^1].flow -= df;
 68         u = edges[p[u]].from;
 69     }
 70     return df;
 71 }
 72 
 73 int max_flow()
 74 {
 75     int flow = 0;
 76     bfs();
 77     memset(num, 0, sizeof(num));
 78     for (int i = 0; i < num_nodes; i++) num[d[i]]++;
 79     int u = source;
 80     memset(cur, 0, sizeof(cur));
 81     while (d[source] < num_nodes) {
 82         if (u == sink) {
 83             flow += augment();
 84             u = source;
 85         }
 86         bool advanced = false;
 87         for (int i = cur[u]; i < G[u].size(); i++) { 
 88             Edge& e = edges[G[u][i]];
 89             if (e.cap > e.flow && d[u] == d[e.to] + 1) {
 90                 advanced = true;
 91                 p[e.to] = G[u][i];
 92                 cur[u] = i;
 93                 u = e.to;
 94                 break;
 95             }
 96         }
 97         if (!advanced) { // retreat
 98             int m = num_nodes - 1;
 99             for ( int i = 0;i < G[u].size();i++)
100                 if (edges[G[u][i]].cap > edges[G[u][i]].flow)
101                     m = min(m, d[edges[G[u][i]].to]);
102             if (--num[d[u]] == 0) break; // gap 优化
103             num[d[u] = m+1]++;
104             cur[u] = 0;
105             if (u != source)
106                 u = edges[p[u]].from;
107         }
108     }
109     return flow;
110 }
111 
112 void addedges(int from,int to,int cap){
113     edges.push_back((Edge){from,to,cap,0});
114     edges.push_back((Edge){to,from,0,0});
115     int m = edges.size();
116     G[from].push_back(m-2);
117     G[to].push_back(m-1);
118 }
119 
120 void init(){
121     edges.clear();
122     for(int i = 0;i < num_nodes;i++) G[i].clear();
123 }
124 
125 int main(){
126     int m;
127     while(scanf("%d %d ",&m,&num_nodes) != EOF){
128         init();
129         source = 0; sink = num_nodes-1;
130         for(int i = 0;i < m;i++){
131             int u,v,c;
132             scanf("%d %d %d",&u,&v,&c);u--;v--;
133             addedges(u,v,c);
134         }
135         int res = max_flow();
136         printf("%d\n",res);
137     }
138     return 0;
139 }
View Code

 

 

 

再存一个dinic的吧~~~

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <stack>
 9 #include <queue>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 #define lp (p << 1)
14 #define rp (p << 1|1)
15 #define getmid(l,r) (l + (r - l) / 2)
16 #define MP(a,b) make_pair(a,b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int INF = 1 << 30;
20 const int maxn = 210;
21 
22 int c[maxn][maxn],n,m,lev[maxn];
23 int st,ed;
24 
25 void Bfs(){
26     memset(lev,-1,sizeof(lev));
27     queue<int> Q;
28     while(!Q.empty()) Q.pop();
29     Q.push(st);
30     lev[st] = 0;
31     while(!Q.empty()){
32         int x = Q.front();Q.pop();
33         for(int i = 1; i <= ed; ++i) if(lev[i] < 0 && c[x][i] > 0){
34             lev[i] = lev[x] + 1;
35             Q.push(i);
36         }
37     }
38 }
39 
40 int Dfs(int p,int minc){
41     if(p == ed) return minc;
42     for(int i = 1; i <= ed; ++i) if(lev[i] > lev[p] && c[p][i] > 0){
43         int d = Dfs(i,min(c[p][i],minc));
44         if(d > 0){
45             c[p][i] -= d;
46             c[i][p] += d;
47             return d;
48         }
49     }
50     return 0;
51 }
52         
53 
54 int Dinic(){
55     int max_flow = 0,plus;
56     while(1){
57         Bfs();
58         if(lev[ed] < 0) break;
59         while((plus = Dfs(1,INF)) > 0) max_flow += plus;
60     }
61     return max_flow;
62 }
63 
64 int main(){
65     while(scanf("%d %d",&m,&n) != EOF){
66         st = 1;ed = n;
67         memset(c,0,sizeof(c));
68         for(int i = 1;i <= m;i++){
69             int u,v,w;
70             scanf("%d %d %d",&u,&v,&w);
71             c[u][v] += w;
72         }
73         printf("%d\n",Dinic());
74     }
75     return 0;
76 }
View Code

 

转载于:https://www.cnblogs.com/wuyuewoniu/p/4715471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值