一、题面

  Drainage Ditches

二、分析

  网络流的裸题。

  1 Edmonds-Karp算法

    求解网络流其实就是一个不断找增广路,然后每次找到一条增广路后更新残余网络的一个过程。

    EK算法主要就是用bfs去找增广路,然后不断更新残余网络得到最终答案。

  2 Dinic算法

    对于Ford-Fulkerson和Edmonds-Karp算法,求解网络流都是一次次用DFS或者BFS,这其实是很费时的,Dinic相当于就是一次BFS然后DFS求解出多条增广路。

  3 ISAP

三、AC代码

 1 //Edmonds-Karp
2 #include <cstdio>
3 #include <iostream>
4 #include <vector>
5 #include <queue>
6 #include <cstring>
7 using namespace std;
8 #define ll long long
9 const int maxn = 2e2 + 13;
10 const int INF = 1e8 + 1;
11 int N, M;
12 int Flow[maxn]; //找到的最小的容量
13 int Pre[maxn]; //记录父节点
14 struct edge
15 {
16 //终点、容量、反边
17 int to, cap, rev;
18 };
19 int G[maxn][maxn];
20 void add_edge(int from, int to, int cap)
21 {
22 G[from][to] += cap;
23 }
24
25 int BFS(int s, int t)
26 {
27 memset(Pre, -1, sizeof(Pre));
28 queue<int> Q;
29 Q.push(s);
30 Pre[s] = -1;
31 Flow[s] = INF;
32 while(!Q.empty())
33 {
34 int q = Q.front();
35 if(q == t)
36 break;
37 Q.pop();
38 for(int i = 1; i <= M; i++)
39 {
40 if(i != s && G[q][i] > 0 && Pre[i] == -1)
41 {
42 Pre[i] = q;
43 Flow[i] = min(Flow[q], G[q][i]);
44 Q.push(i);
45 }
46 }
47 }
48 if(Pre[t] == -1)
49 return 0;
50 else
51 return Flow[t];
52 }
53
54 ll EK(int s, int t)
55 {
56 ll ans = 0;
57 int f = BFS(s, t);
58 while(f)
59 {
60 ans += f;
61 int p = t;
62 while(Pre[p] != -1)
63 {
64 G[Pre[p]][p] -= f;
65 G[p][Pre[p]] += f;
66 p = Pre[p];
67 }
68 f = BFS(s, t);
69 }
70 return ans;
71 }
72
73 int main()
74 {
75 freopen("input.txt", "r", stdin);
76 while(scanf("%d%d", &N, &M) != EOF)
77 {
78 memset(G, 0, sizeof(G));
79 int from, to, cap;
80 for(int i = 0; i < N; i++)
81 {
82 scanf("%d%d%d", &from, &to, &cap);
83 add_edge(from, to, cap);
84 }
85 printf("%I64d\n", EK(1, M));
86 }
87 return 0;
88 }
  1 //Dinic
2 #include <cstdio>
3 #include <cstring>
4 #include <iostream>
5 #include <algorithm>
6 #include <vector>
7 #include <queue>
8
9 using namespace std;
10 #define ll long long
11 #define Min(a,b) ((a)>(b)?(b):(a))
12 #define Max(a,b) ((a)>(b)?(a):(b))
13 const int INF = 1e9;
14 const int maxn = 5e2 + 13;
15 struct edge
16 {
17 int to, cap, rev;
18 };
19 vector<edge> G[maxn];
20 int N, M;
21 int level[maxn], iter[maxn];
22 //iter数组的作用就是记录DFS时该结点已经访问到的邻接点的位置
23 void add_edge(int from, int to, int cap)
24 {
25 G[from].push_back( (edge){to, cap, G[to].size()});
26 G[to].push_back( (edge){from, 0, G[from].size() - 1});
27 }
28
29 void BFS(int s)
30 {
31 memset(level, -1, sizeof(level));
32 queue<int> que;
33 level[s] = 0;
34 que.push(s);
35 while(!que.empty())
36 {
37 int v = que.front();
38 que.pop();
39 for(int i = 0; i < G[v].size(); i++)
40 {
41 edge &e = G[v][i];
42 if(e.cap > 0 && level[e.to] < 0)
43 {
44 level[e.to] = level[v] + 1;
45 que.push(e.to);
46 }
47 }
48 }
49 }
50
51 int DFS(int s, int t, int f)
52 {
53 if(s == t)
54 return f;
55 for(int &i = iter[s]; i < G[s].size(); i++)
56 {
57 edge &e = G[s][i];
58 if(e.cap > 0 && level[s] < level[e.to])
59 {
60 int d = DFS(e.to, t, Min(f, e.cap));
61 if(d > 0)
62 {
63 e.cap -= d;
64 G[e.to][e.rev].cap += d;
65 return d;
66 }
67 }
68 }
69 return 0;
70 }
71
72 int Dinic(int s, int t)
73 {
74 int flow = 0;
75 while(1)
76 {
77 BFS(s);
78 if(level[t] < 0)
79 return flow;
80 memset(iter, 0, sizeof(iter));
81 int f = DFS(s, t, INF);
82 while(f > 0)
83 {
84 flow += f;
85 f = DFS(s, t, INF);
86 }
87 }
88 }
89
90 int main()
91 {
92 //freopen("input.txt", "r", stdin);
93 int from, to, cap;
94 while(~scanf("%d%d", &N, &M))
95 {
96 for(int i = 0; i < N; i++)
97 {
98 scanf("%d%d%d", &from, &to, &cap);
99 add_edge(from, to, cap);
100 }
101 printf("%d\n", Dinic(1, M));
102 for(int i = 1; i < M; i++)
103 {
104 G[i].clear();
105 }
106 }
107 return 0;
108 }
  1 //ISAP
2 #include <cstdio>
3 #include <cstring>
4 #include <iostream>
5 #include <algorithm>
6 #include <vector>
7 #include <queue>
8
9 using namespace std;
10 #define ll long long
11 #define Min(a,b) ((a)>(b)?(b):(a))
12 #define Max(a,b) ((a)>(b)?(a):(b))
13 const int INF = 1e9;
14 const int maxn = 2e2 + 13;
15 struct edge{
16 int v, w, nxt;
17 } e[maxn<<1];
18 int h[maxn], tot, n, m, gap[maxn], last[maxn], d[maxn],que[maxn], ql, qr;
19 vector<int> inv[maxn];
20
21 int read() {
22 int x=0,f=1;
23 char c=getchar();
24 for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
25 for (;isdigit(c);c=getchar()) x=x*10+c-'0';
26 return x*f;
27 }
28
29 void add(int u, int v, int w)
30 {
31 e[++tot] = (edge){v, w, h[u]};
32 h[u] = tot;
33 e[++tot] = (edge){u, 0, h[v]};
34 h[v] = tot;
35 }
36
37 void init(int s, int t)
38 {
39 memset(gap, 0, sizeof(gap));
40 memset(d, 0, sizeof(d));
41 ++gap[d[t] = 1];
42 for(int i = 1; i <= n; i++)
43 last[i] = h[i];
44 que[ql = qr = 1] = t;
45 while(ql <= qr)
46 {
47 int x = que[ql++];
48 for(int i = h[x], v = e[i].v;i; i = e[i].nxt, v = e[i].v)
49 {
50 if(!d[v])
51 {
52 ++gap[d[v] = d[x] + 1];
53 que[++qr] = v;
54 }
55 }
56 }
57 }
58
59 int aug(int x, int s, int t, int mi)
60 {
61 if(x == t)
62 return mi;
63 int flow = 0;
64 for(int &i = last[x], v = e[i].v;i;i = e[i].nxt, v = e[i].v)
65 {
66 if(d[x] == d[v] + 1)
67 {
68 int tmp = aug(v, s, t, min(mi, e[i].w));
69 flow += tmp;
70 mi -= tmp;
71 e[i].w -= tmp;
72 e[i^1].w += tmp;
73 if(!mi)
74 return flow;
75 }
76 }
77 if(!(--gap[d[x]]))
78 d[s] = n + 1;
79 ++gap[++d[x]];
80 last[x] = h[x];
81 return flow;
82 }
83
84 int maxflow(int s, int t)
85 {
86 init(s, t);
87 int ret = aug(s, s, t, INF);
88 while(d[s] <= n)
89 ret += aug(s, s, t, INF);
90 return ret;
91 }
92
93 int main()
94 {
95 freopen("input.txt", "r", stdin);
96 while(~scanf("%d%d", &m, &n))
97 {
98 tot = 1;
99 memset(h, 0, sizeof(h));
100 for(int i = 1; i <= n; i++)
101 inv[i].clear();
102 for(int i = 1; i <= m; i++)
103 {
104 int u = read();
105 int v = read();
106 int w = read();
107 add(u, v, w);
108 if(w)
109 inv[v].push_back(u);
110 }
111 int ans = maxflow(1, n);
112 printf("%d\n", ans);
113 }
114 return 0;
115 }

POJ_1273 Drainage Ditches 【网络流】的更多相关文章

  1. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  2. POJ 1273:Drainage Ditches 网络流模板题

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63339   Accepted: 2443 ...

  3. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  4. USACO 4.2 Drainage Ditches(网络流模板题)

    Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's fa ...

  5. NYOJ 323 Drainage Ditches 网络流 FF 练手

    Drainage Ditches 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Every time it rains on Farmer John's fields, ...

  6. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  7. Drainage Ditches~网络流模板

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  8. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  9. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

随机推荐

  1. Git使用指南(上)

    1 Git简介 学习一门技术老师更加倾向于看官网的. 度娘看完了,官网看完了,大家还是很懵逼 学生成绩管理系统 登录模块   3.2 登录模块进一步完善    缺一个验证码的功能    3.3 登录模 ...

  2. POJ 2923 Relocation(状压DP)题解

    题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...

  3. RT-Thread学习笔记2-互斥量与信号量

    目录 1. 临界区保护 1.1 方法一:关闭系统调度保护临界区 1.2 方法二:互斥特性保护临界区 2. 信号量 2.1 信号量的定义 2.2 信号量的操作 3. 生产者.消费者问题 4. 互斥量 4 ...

  4. How to change Linux Terminal display username

    How to change Linux Terminal display username 如何更改 Linux Terminal 显示的用户名 (base) ➜ ~ whoami xgqfrms-m ...

  5. Express All In One

    Express All In One express.js, node.js web framework # v4.17.1 Latest, on May 26, 2019 $ yarn add ex ...

  6. 手机 wifi 已连接,不可上网 bug

    手机 wifi 已连接,不可上网 bug 同一个 Wi-Fi,电脑却可以? 注意事项 Mac 共享热点支持有线连接.蓝牙连接的网络进行共享. 如果你的 Mac 本身是通过 wifi 来连接上网的,那就 ...

  7. vue render html string

    vue render html string shit element ui render string array relativeShowConvert(data) { // log(`data` ...

  8. how to export svg form web page in js

    how to export svg form web page in js https://stackoverflow.com/questions/2483919/how-to-save-svg-ca ...

  9. Flutter: MobX和flutter_mobx状态管理器

    MobX.dart网站上的 " 入门指南" mobxjs video 组织Stores 安装依赖 dependencies: mobx: flutter_mobx: dev_dep ...

  10. 为什么 Python 的 f-string 可以连接字符串与数字?

    本文出自"Python为什么"系列,归档在 Github 上:https://github.com/chinesehuazhou/python-whydo 毫无疑问,Python ...