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),sizeof((a)))
 8 using namespace std;
 9
10 const int N = 201;
11 const int inf = 0x3f3f3f3f;
12 int n, m;
13 int g[N][N];//i到j的容量
14 int pre[N];//i的前驱节点
15 bool vis[N];
16 int flow[N][N];//从i到j的流量
17 int a[N];//从源点到i节点的最小残留量
18
19 int Edmonds_Karp(int st, int ed){
20     int u, v;
21     int f = 0;//最大流
22     queue<int>q;//bfs找增广路
23     while(1){
24         CLR(vis, 0); CLR(a, 0);//每找一次记得初始化
25         q.push(st);
26         vis[st] = 1;
27         a[st] = inf;
28         while(!q.empty()){
29             u = q.front(); q.pop();
30             for(v = 1; v <= n; ++v){
31                 if(!vis[v] && g[u][v] > flow[u][v]){
32                     q.push(v);
33                     pre[v] = u;
34                     vis[v] = 1;
35                     a[v] = min(a[u], g[u][v] - flow[u][v]);
36                 }
37             }
38         }
39         if(a[ed] == 0) break;//当前已经是最大流
40         f += a[ed];
41         for(u = ed; u != st; u = pre[u]){
42             flow[pre[u]][u] += a[ed];
43             flow[u][pre[u]] -= a[ed];
44         }
45
46     }
47     return f;
48 }
49 int main(){
50     int i, j, k, a, b, c;
51     while(scanf("%d %d", &m, &n)==2){
52         CLR(g, 0); CLR(flow, 0);
53         for(i = 0; i < m; ++i){
54             scanf("%d %d %d", &a, &b, &c);
55             g[a][b] += c;//可能有重边
56         }
57         int max_flow = Edmonds_Karp(1, n);
58         printf("%d\n", max_flow);
59     }
60     return 0;
61 }

时间: 2024-10-22 04:36:37

Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )的相关文章

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

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 算法: 最大流之增广路(入门)

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

[POJ 1273] Drainage Ditches &amp; 最大流Dinic模板

Drainage Ditches 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

poj 1273 Drainage Ditches(最大流入门)

题意:n个池塘,m条水渠,求从第一个池塘到第m个池塘能运送的最大流量: 思路:裸最大流dicnic算法.建分层图并不断找增广路,直到找不到增广路即为最大流. 邻接表实现: #include <cstdio> #include <cstring> #include <queue> using namespace std; #define MAXN 210 #define INF 0x3f3f3f3f struct Edge { int st, ed; int c; int

POJ 1273 Drainage Ditches | 最大流模板

1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<queue> 5 #define N 210 6 #define INF 1234567897 7 using namespace std; 8 int cnt=2,head[N],n,m,ans,lev[N]; 9 queue <int> q; 10 struct edge 11 { 12 int u,

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 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