hdu1532Drainage Ditches【最大流】

Drainage Ditches


Time Limit: 2000/1000 MS
(Java/Others)    Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 8431    Accepted
Submission(s): 3921

Problem Description

Every time it rains on Farmer John‘s fields, a pond
forms over Bessie‘s favorite clover patch. This means that the clover is covered
by water for awhile and takes quite a long time to regrow. Thus, Farmer John has
built a set of drainage ditches so that Bessie‘s clover patch is never covered
in water. Instead, the water is drained to a nearby stream. Being an ace
engineer, Farmer John has also installed regulators at the beginning of each
ditch, so he can control at what rate water flows into that
ditch. 
Farmer John knows not only how many gallons of water each ditch
can transport per minute but also the exact layout of the ditches, which feed
out of the pond and into each other and stream in a potentially complex
network. 
Given all this information, determine the maximum rate at
which water can be transported out of the pond and into the stream. For any
given ditch, water flows in only one direction, but there might be a way that
water can flow in a circle.

Input

The input includes several cases. For each case, the
first line contains two space-separated integers, N (0 <= N <= 200) and M
(2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is
the number of intersections points for those ditches. Intersection 1 is the
pond. Intersection point M is the stream. Each of the following N lines contains
three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the
intersections between which this ditch flows. Water will flow through this ditch
from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which
water will flow through the ditch.

Output

For each case, output a single integer, the maximum
rate at which water may emptied from the pond.

Sample Input

5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10

Sample Output

50

Source

USACO
93

题意:

最大流

分析:

最大流

代码:

  1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5 #include <vector>
6 using namespace std;
7
8 const int maxn = 205 << 1;
9 const int INF = 1000000000;
10
11 struct Edge
12 {
13 int from, to, cap, flow;
14 };
15
16 struct Dinic
17 {
18 int n, m, s, t;
19 vector<Edge> edges;
20 vector<int>G[maxn];
21 bool vis[maxn];
22 int d[maxn];
23 int cur[maxn];
24
25 void ClearAll(int n) {
26 for(int i = 0; i <= n; i++) {
27 G[i].clear();
28 }
29 edges.clear();
30 }
31
32 void AddEdge(int from, int to, int cap) {
33 edges.push_back((Edge){from, to, cap, 0} );
34 edges.push_back((Edge){to, from, 0, 0} );
35 m = edges.size();
36 G[from].push_back(m - 2);
37 G[to].push_back(m - 1);
38 //printf("%din end\n",m);
39 }
40
41 bool BFS()
42 {
43 memset(vis, 0, sizeof(vis) );
44 queue<int> Q;
45 Q.push(s);
46 vis[s] = 1;
47 d[s] = 0;
48 while(!Q.empty() ){
49 int x = Q.front(); Q.pop();
50 for(int i = 0; i < G[x].size(); i++) {
51 Edge& e = edges[G[x][i]];
52 if(!vis[e.to] && e.cap > e.flow) {
53 vis[e.to] = 1;
54 d[e.to] = d[x] + 1;
55 Q.push(e.to);
56 }
57 }
58 }
59 return vis[t];
60 }
61
62 int DFS(int x, int a) {
63 if(x == t || a == 0) return a;
64 int flow = 0, f;
65 for(int& i = cur[x]; i < G[x].size(); i++) {
66 Edge& e = edges[G[x][i]];
67 if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
68 e.flow += f;
69 edges[G[x][i]^1].flow -= f;
70 flow += f;
71 a -= f;
72 if(a == 0) break;
73 }
74 }
75 return flow;
76 }
77
78 int Maxflow(int s, int t) {
79 this -> s = s; this -> t = t;
80 int flow = 0;
81 while(BFS()) {
82 memset(cur, 0, sizeof(cur) );
83 flow += DFS(s, INF);
84 }
85 return flow;
86 }
87 };
88
89 int main() {
90 int n, m;
91 int a, b, c;
92 while(EOF != scanf("%d %d",&n, &m)) {
93 Dinic g;
94 for(int i = 0; i < n; i++) {
95 scanf("%d %d %d",&a, &b, &c);
96 g.AddEdge(a, b, c);
97 }
98 printf("%d\n",g.Maxflow(1, m) );
99 }
100 return 0;
101 }

hdu1532Drainage Ditches【最大流】,布布扣,bubuko.com

时间: 2024-10-25 18:58:39

hdu1532Drainage Ditches【最大流】的相关文章

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

hdu 1532 Drainage Ditches(最大流)

Drainage Ditches Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drai

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is cover

poj 1273 Drainage Ditches (最大流入门)

1 /****************************************************************** 2 题目: Drainage Ditches(POJ 1273) 3 链接: http://poj.org/problem?id=1273 4 题意: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条 5 水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水 6 渠中所能流过的水的最大容量.水流是单向的. 7 算法: 最大流之增广路(入门)

HDU 1532Drainage Ditches(最大流模板题 ISAP)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11306    Accepted Submission(s): 5328 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie'

Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... 1 #include<cstdio> 2 #include<vector> 3 #include<queue> 4 #include<cstring> 5 #include<set> 6 #include<algorithm> 7 #define CLR(a,b) memset((a),(b),si

poj 1273 Drainage Ditches 最大流入门题

题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has

TOJ 4085 Drainage Ditches(最大流)

描述 Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches s

HDU 1532 Drainage Ditches(最大流 EK算法)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用"+="替换"=". 对网络流不熟悉的,给一篇讲解:http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html. ?(? ? ??)我是看这篇博客才入门的. 代码: 1 #include <cstdio> 2 #includ