poj 1273 Drainage Ditches(最大流)
2015-08-13 10:55
381 查看
http://poj.org/problem?id=1273
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 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
Sample Output
View Code
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 62708 | Accepted: 24150 |
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个点,求1到n的最大流量 Dinic模板:
#include<stdio.h> #include<algorithm> #include<queue> #include<string.h> #define INF 0x3f3f3f3f #define N 1010 using namespace std; struct Edge { int u, v, next, flow; } edge[N * N]; int layer , head , cnt; void Init() { memset(head, -1, sizeof(head)); cnt = 0; } void AddEdge(int u, int v, int flow) { edge[cnt].u = u; edge[cnt].v = v; edge[cnt].flow = flow; edge[cnt].next = head[u]; head[u] = cnt++; } bool BFS(int Start, int End)//分层 { queue<int>Q; memset(layer, -1, sizeof(layer)); Q.push(Start); layer[Start] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); if(u == End) return true; for(int i = head[u] ; i != -1 ; i = edge[i].next) { int v = edge[i].v; if(layer[v] == -1 && edge[i].flow > 0) { layer[v] = layer[u] + 1; Q.push(v); } } } return false; } int DFS(int u, int Maxflow , int End) { if(u == End) return Maxflow; int uflow = 0; for(int i = head[u] ; i != -1 ; i = edge[i].next) { int v = edge[i].v; if(layer[v] == layer[u] + 1 && edge[i].flow > 0) { int flow = min(edge[i].flow, Maxflow - uflow); flow = DFS(v, flow, End); edge[i].flow -= flow; edge[i^1].flow += flow; uflow += flow; if(uflow == Maxflow) break; } } if(uflow == 0) layer[u] = 0; return uflow; } int Dinic(int Start, int End) { int Maxflow = 0; while(BFS(Start, End)) Maxflow += DFS(Start, INF, End); return Maxflow; } int main() { int m, n; while(~scanf("%d%d", &m, &n)) { Init(); int u, v, flow; while(m--) { scanf("%d%d%d", &u, &v, &flow); AddEdge(u, v, flow); AddEdge(v, u, 0); } printf("%d\n", Dinic(1, n)); } return 0; }
View Code
相关文章推荐
- AI:确定性AI
- Climbing Stairs
- [CodeForces 300D Painting Square]DP
- zoj 3175 Number of Containers 分块加速
- hdu1848 Fibonacci again and again
- 运项目难点之ScrollView中嵌套百度地图(BaiduMap)的解决方案
- C - ACboy needs your help again!
- 利用Baidu API正向和反向解析地址
- 模糊专家系统概述
- Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7
- yield() sleep()wait()区别
- B Black Square----(2015 summer training #5)
- 2015 Multi-University Training Contest 3 hdu 5324 Boring Class
- http://blog.csdn.net/vichie2008/article/details/40823531
- POJ 1903 Jurassic Remains -- 中途相遇法
- UVa 10651 Pebble Solitaire(状态压缩DP)
- Fractions Again?!
- 2015 HUAS Summer Trainning #5~J
- Factorial Trailing Zeroes
- 访问网站出现:HTTP Error 503. The service is unavailable