POJ1273(最大流)

Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 70451   Accepted: 27391

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

最大流入门题,裸的Edmonds-Karp。
 1 //2016.9.22
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 #define N 205
 7
 8 using namespace std;
 9
10 const int inf = 0x3f3f3f3f;
11
12 struct Edge
13 {
14     int from, to , cap, flow;//分别为边的两个端点、容量、边上的流
15     Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f){}
16 };
17
18 struct EdmondsKarp//Edmonds-Karp算法
19 {
20     int n, m;//结点编号0——n-1
21     vector<Edge> edges;//存边与反向弧
22     vector<int> G[N];//邻接表,G[i][j]表示结点i的第j条边在数组edges中的序号
23     int a[N];//当起点到i的可改进量
24     int p[N];//最短路树上p的入弧编号
25
26     void init(int n)//初始化
27     {
28         for(int i = 0; i < n; i++)G[i].clear();
29         edges.clear();
30     }
31
32     void addEdge(int from, int to, int cap)//添加边
33     {
34         edges.push_back(Edge(from, to, cap, 0));
35         edges.push_back(Edge(to, from, 0, 0));//反向弧
36         m = edges.size();
37         G[from].push_back(m-2);//加入编号
38         G[to].push_back(m-1);
39     }
40
41     int maxFlow(int s, int t)//最大流,s为源点,t为汇点
42     {
43         int flow = 0;
44         while(1)
45         {
46             memset(a, 0, sizeof(a));
47             queue<int> Q;
48             Q.push(s);
49             a[s] = inf;
50             while(!Q.empty())
51             {
52                 int x = Q.front(); Q.pop();
53                 for(int i = 0; i < G[x].size(); i++)
54                 {
55                     Edge& e = edges[G[x][i]];
56                     if(!a[e.to] && e.cap>e.flow)
57                     {
58                         p[e.to] = G[x][i];
59                         a[e.to] = min(a[x], e.cap-e.flow);
60                         Q.push(e.to);
61                     }
62                 }
63                 if(a[t])break;
64             }
65             if(!a[t])break;//残余网络中不存在增广路,当前流是最大流
66             for(int u = t; u != s; u = edges[p[u]].from)//找到一条增广路
67             {
68                 edges[p[u]].flow += a[t];
69                 edges[p[u]^1].flow -= a[t];//p[u]^1为p[u]的反向边
70             }
71             flow += a[t];
72         }
73         return flow;
74     }
75 };
76
77 int main()
78 {
79     int n, m;
80     EdmondsKarp e;
81     while(scanf("%d%d", &m, &n)!=EOF)
82     {
83         int u, v, c;
84         e.init(n);
85         for(int i = 0; i < m; i++)
86         {
87             scanf("%d%d%d", &u, &v, &c);
88             u--, v--;
89             e.addEdge(u, v, c);
90         }
91         printf("%d\n", e.maxFlow(0, n-1));
92     }
93
94     return 0;
95 }
时间: 2024-07-31 22:48:21

POJ1273(最大流)的相关文章

poj1273最大流dinc

bfs 构建层次图,dfs 寻找增广路.dfs在寻找增广路的同时自我调整直到此时的层次图无增广路时 重新构图,直到无增广路为止. 对于添加反弧,觉得对于每点 进流量和 出流量应该守恒,反向弧的添加方便自我调整,而通过每点的流量没变,最后导致流到终点的流量不变. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string>

POJ1273 最大流 EK算法

套了个EK的模板 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <climits> #include <cstring> #include <cmath> #include <stack> #include <queue> #i

POJ1273 最大流模板

之前自己写的,以后当一个模板的基础吧,不管是最大流,最小割,二分图匹配 下面是POJ1273的一个裸题.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 struct node{ 8 int to,cap,rev; 9 node(int _t

POJ1273 最大流EK 裸题

1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 int n,m; 7 const int maxn = 205; 8 int c[maxn][maxn],flow[maxn][maxn]; 9 int vis[maxn],fa[maxn]; 10 int largest_flow(int s,

poj1273 Drainage Ditches(裸最大流)

Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Every time it rains on Farmer Joh

[笔记] 网络流-最大流 POJ-1273\HDU-4240

[1] POJ-1273 题目:http://poj.org/problem?id=1273 最直接的最大流问题,用了Ford-Fulkerson方法,DFS随机搜索增广路. 算法原理参考:http://blog.csdn.net/smartxxyx/article/details/9293665 /************************ POJ-1273* Ford-Fulkerson***********************/#include <stdio.h> #inclu

HDU 1532||POJ1273:Drainage Ditches(最大流)

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

最大流的理解以及dinic模板 poj1273

增广路以及残留网络的定义不再赘述了.算导上说的很清楚,证明也有,看懂了就知道怎么求最大流了. 而算导上提到的FF方法以及ek算法的伪代码中都是将流与残留容量分开储存,其实代码实现的时候我们只需存正反向弧的残留容量即可. 然后是对残留网络的一些理解,残留网络中的反向弧是怎么来的? 残留网络的每条边都是这条有向边的残留容量,而残留容量又由公式cf(u,v)=c(u,v)-f(u,v)得到,那么对于一条不存在的有向边(v,u),其容量c(v,u)=0,f(v,u)=-f(u,v)通过反对称性可知,那么

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