HDU 1532-Drainage Ditches(网络最大流,EK,Ford-Fulkerson,dinic3种算法套模板)

来源:互联网 发布:navicat for mysql破解 编辑:程序博客网 时间:2024/05/15 19:13

Drainage Ditches

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


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 41 2 401 4 202 4 202 3 303 4 10
 

Sample Output
50
//网络最大流EK算法//纪念第一次使用网络最大流#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#define maxn 205#define inf 0x3f3f3f3fusing namespace std;int r[maxn][maxn];int pre[maxn];bool vis[maxn];int n, m;bool bfs(int s, int t){queue<int>q;q.push(s);memset(pre, 0, sizeof(pre));memset(vis, false, sizeof(vis));pre[s] = s;while(!q.empty()){int p = q.front();q.pop();vis[p] = true;for(int i=1; i<=m; i++){if(r[p][i]>0 && !vis[i]){pre[i] = p;vis[i] = true;if(i==t)return true;q.push(i);}}}return false;}int EK(int s, int t){int f = 0;//不断的找增广路径while(bfs(s, t)){int d = inf;for(int i=t; i!=s; i=pre[i]){d = min(d, r[pre[i]][i]);}//残余网络, 即反向边for(int i=t; i!=s; i=pre[i]){r[pre[i]][i] -= d;r[i][pre[i]] += d;}f += d;}return f;}int main(){while(scanf("%d%d", &n, &m)!=EOF){memset(r, 0, sizeof(r));for(int i=0; i<n; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);r[u][v] += w;}int ans = EK(1, m);printf("%d\n", ans);}return 0;}

//网络最大流Ford-Fulkerson算法,邻接表实现#include <stdio.h>#include <string.h>#include <vector>#include <algorithm>#define inf 0x3f3f3f3f#define maxn 205using namespace std;int vis[maxn];int n, m;struct edge{int to, w, rev;    //终点, 容量, 反向边};vector<edge> G[maxn];void add_edge(int u, int v, int w){G[u].push_back((edge){v, w, G[v].size()});G[v].push_back((edge){u, 0, G[u].size()-1});}int dfs(int s, int t, int f){    if(s==t)        return f;vis[s] = 1;for(int i=0; i<G[s].size(); i++){if(G[s][i].w>0 && !vis[G[s][i].to]){int d = dfs(G[s][i].to, t, min(f, G[s][i].w));if(d>0){G[s][i].w -= d;G[G[s][i].to][G[s][i].rev].w += d;return d;}}}return 0;}int max_flow(int s, int t){int f = 0;for(;;){memset(vis, 0, sizeof(vis));int d = dfs(s, t, inf);if(d==0)return f;f += d;}}int main(){while(scanf("%d%d", &n, &m)!=EOF){    for(int i=0; i<maxn; i++)            G[i].clear();for(int i=0; i<n; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);add_edge(u, v, w);}printf("%d\n", max_flow(1, m));}return 0;}

//dinic算法#include <stdio.h>#include <string.h>#include <algorithm>#include <vector>#include <queue>#define maxn 205#define inf 0x3f3f3f3fusing namespace std;struct edge{int to, w, rev;};vector<edge> G[maxn];int level[maxn];int iter[maxn];int n, m;void add_edge(int s, int t, int w){G[s].push_back((edge){t, w, G[t].size()});G[t].push_back((edge){s, 0, G[s].size()-1});}int dfs(int v, int t, int f){if(v==t)return f;for(int &i=iter[v]; i<G[v].size(); i++){edge &e = G[v][i];if(level[e.to]>level[v] && e.w>0){int d = dfs(e.to, t, min(f, e.w));if(d>0){    e.w -= d;                G[e.to][e.rev].w += d;                return d;}}}return 0;}void bfs(int s){level[s] = 0;queue<int> q;q.push(s);while(!q.empty()){int v = q.front();q.pop();for(int i=0; i<G[v].size(); i++){edge &e = G[v][i];if(level[e.to]<0 && e.w>0){level[e.to] = level[v] + 1;q.push(e.to);}}}}int max_flow(int s, int t){int flow = 0;for(;;){int f = 0;    memset(level, -1, sizeof(level));bfs(s);if(level[t]<0)return flow;memset(iter, 0, sizeof(iter));while((f = dfs(s, t, inf))>0){flow += f;}}}int main(){while(~scanf("%d%d", &n, &m)){for(int i=0; i<maxn; i++)G[i].clear();for(int i=0; i<n; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);add_edge(u, v, w);}printf("%d\n", max_flow(1, m));}return 0;}


阅读全文
0 0
原创粉丝点击