Drainage Ditches POJ1273
試題鏈接
文章目錄
- Description
- 題意:
- 題解:
- 代碼:
- Dinic做法
- EK做法
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 10Sample Output
50題意:
n個邊,m個點,其中點1是進水點,點m是出水點,每個邊都有流水速率,問水流出的最大速率是多少?
題目樣例分析如圖:
1->4 流速為20
1->2->4 流速為20
1->2->3->4 流速為10
最終答案為50
題解:
典型的最大流問題
最大流的算法有很多,有FF算法,EK,Dinic,ISAP等
模板題,可以通過這個練練手入門
網絡流非詳細講解
代碼:
改了好幾次終于改對了
題目中說的有好幾組數據。。所以while讀入
代碼里面有比較詳細的注釋
Dinic做法
兩個流程:
一個是bfs分層&&判斷是否還有增廣路
另一個dfs找最大流
EK做法
#include <cstdio> #include <algorithm> #include <queue> #include <string.h> using namespace std; int const MAX = 1005; int const inf = 0x3f3f3f3f; int c[MAX][MAX];//c[u][v]保存容量 int f[MAX][MAX];//f[u][v]保存當前流量 int a[MAX];// a數組在每趟bfs中找到最小路徑中最小殘余流量的,a數組是個遞推數組,a[v]的意思是從源點s到點v的最小殘余流量、 //同時a數組還可以判斷一個點是否遍歷過 int pre[MAX];//保存前一個點 int n, m; int bfs(int s, int t) {queue<int> q;int flow = 0;while(!q.empty()) q.pop();memset(f, 0, sizeof(f));while(1){memset(a, 0, sizeof(a));a[s] = inf;//將起始點的最小殘余量設為最大q.push(s);while(!q.empty()){//bfs找到一條最短路,這里的邊不代表距離,可以看作每兩個點都是單位距離的int u;u = q.front();q.pop();for(int v = 1; v <= m; v++){//枚舉所有點v <u,v>if(!a[v] && c[u][v] > f[u][v]){//a[]可以代替vis[],來判斷這個點是否已經遍歷過,后面那個條件更是起了關鍵作用,很巧妙pre[v] = u;q.push(v);a[v] = min(a[u], c[u][v] - f[u][v]);//遞推}}}if(!a[t]) break;//直到最小殘余流量為0時,退出for(int u = t; u != s; u = pre[u]){//更新增廣路上的流量 f[pre[u]][u] += a[t];// f[u][pre[u]] -= a[t];//反向邊增加 }flow += a[t];//增加整個增廣路的流量 }return flow; }int main() {while(~scanf("%d %d", &n, &m)){memset(c, 0, sizeof(c));memset(pre, 0, sizeof(pre));for(int i = 1; i <= n; i++){int u, v, w;scanf("%d %d %d", &u, &v, &w);c[u][v] += w;}printf("%d\n", bfs(1, m));}return 0; }總結
以上是生活随笔為你收集整理的Drainage Ditches POJ1273的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qq群 怎么推广(qq群怎么推广让别人加
- 下一篇: 怎么往百度地图上传地址(怎么往百度地图上