POJ 1273 --Drainage Ditches【最大流模板 && dinic】
来源:互联网 发布:美工学徒招聘是真的吗 编辑:程序博客网 时间:2024/05/22 04:46
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 62111 Accepted: 23859
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.
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
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#define maxn 1000 + 100#define maxm 2000 + 200#define INF 0x3f3f3f3fusing namespace std;int cnt, n, m;int dist[maxn];int cur[maxn], vis[maxn];int head[maxn];struct node { int u, v, cap, flow, next;};node edge[maxm];void init(){ cnt = 0; memset(head, -1, sizeof(head));}void add(int u,int v, int w){ edge[cnt] = {u, v, w, 0, head[u]}; //正建边 head[u] = cnt++; edge[cnt] = {v, u, 0, 0, head[v]}; //反建边 head[v] = cnt++;}void getmap(){ int u, v, w; while(m--){ scanf("%d%d%d", &u, &v, &w); add(u, v, w); } return ;}bool BFS(int st,int ed){ queue<int>q; memset(dist, -1, sizeof(dist)); memset(vis, 0, sizeof(vis)); q.push(st); dist[st] = 0; vis[st] = 1; while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(!vis[E.v] && E.cap > E.flow){ vis[E.v] = 1; dist[E.v] = dist[u] + 1; if(E.v == ed) return true; q.push(E.v); } } } return false;}int DFS(int x, int ed, int a){ if(x == ed || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next){ node &E = edge[i]; if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){ E.flow += f; edge[i ^ 1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow;}int maxflow(int st, int ed){ int flow = 0;; while(BFS(st, ed)){ memcpy(cur, head, sizeof(head)); flow += DFS(st, ed, INF); } return flow;}int main (){ while(scanf("%d%d", &m, &n) != EOF){ init(); getmap(); printf("%d\n",maxflow(1, n)); } return 0;}
0 0
- POJ 1273 --Drainage Ditches【最大流模板 && dinic】
- 【poj 1273】Drainage Ditches 最大流dinic模板
- POj 1273 Drainage Ditches (最大流 Dinic + ISAP 模板)
- poj 1273 Drainage Ditches--最大流--Dinic
- poj 1273 Drainage Ditches (最大流Dinic)
- POJ 1273 Drainage Ditches 最大流 dinic
- POJ-1273 Drainage Ditches 最大流Dinic
- poj 1273 Drainage Ditches【最大流 dinic】
- POJ--1273--Drainage Ditches【Dinic模板】【isap模板】网络最大流
- HDU1532 Drainage Ditches 最大流Dinic模板
- POJ 1273 Drainage Ditches(最大流模板)
- POJ 1273 Drainage Ditches (dinic模板)
- POJ 1273 Drainage Ditches(dinic模板)
- POJ 1273 Drainage Ditches—网络流,最大流模板题,Dinic算法
- poj 1273 Drainage Ditches(最大流E-K +dinic 模板)
- poj 1273 & hdu 1532 Drainage Ditches(最大流 )EK,dinic模板
- POJ-1273-Drainage Ditches(最大流)dinic实现 后续模板待补充
- poj 1273 Drainage Ditches(最大流 dinic模版)
- 支付宝接口应用及错误整理
- 南邮 OJ 1032 三角形数
- Stagefright多媒体架构中的A31的OMX插件和Codec组件
- p1501 二叉树最大宽度和高度
- [深入理解Android卷二 全文-第四章]深入理解PackageManagerService
- POJ 1273 --Drainage Ditches【最大流模板 && dinic】
- 从优化到再优化,最长公共子串
- jsp使用cookie实现记住密码的功能
- 用StringBuilder(StringBuffer)#append替代字符串”+”会带来性能提升吗
- 从优化到再优化,最长公共子串
- Android 微信下拉眼睛
- 从优化到再优化,最长公共子串
- django rest framework quickstart
- 从优化到再优化,最长公共子串