POJ 1273 Drainage Ditches(最大流模板)

    xiaoxiao2025-11-15  1

    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

    题意:M条沟渠,N个池塘,求最大水流量。

    记得初始化vector。。。

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> #include <iomanip> #include <cstdlib> #include <string.h> #include <vector> #include <queue> #include <stack> #include <ctype.h> using namespace std; const int MAX_V = 205; const int INF = 0x7FFFFFFF; struct edge { int to; //终点 int cap; //容量 int rev; //反向边 edge() {} edge(int t, int c, int r) { to = t; cap = c; rev = r; } }; vector<edge> G[MAX_V]; //图的邻接表 bool used[MAX_V]; //DFS中用到的访问标记 //向图中增加一条从s到t容量为cap的边 void add_edge(int from, int to, int cap) { G[from].push_back(edge(to, cap, (int)G[to].size())); G[to].push_back(edge(from, 0, (int)G[from].size() - 1)); } //通过DFS寻找增广路 int dfs(int v, int t, int f) { if(v == t) return f; used[v] = true; for(int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if(!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if(d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } //求解从s到t的最大流 int max_flow(int s, int t) { int flow = 0; for( ; ; ) { memset(used, 0, sizeof(used)); int f = dfs(s, t, INF); if(f == 0) return flow; flow += f; } } int main() { int m, n; while(scanf("%d%d", &m, &n) != EOF) { for(int i=0;i<=200;i++) G[i].clear(); for(int i = 0; i < m; i++) { int from, to, cap; scanf("%d%d%d", &from, &to, &cap); add_edge(from, to, cap); } cout<<max_flow(1,n)<<endl; } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1304225.html
    最新回复(0)