日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Drainage Ditches POJ1273

發布時間:2023/12/3 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Drainage Ditches POJ1273 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 93263 Accepted: 36174

試題鏈接

文章目錄

    • 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 10

Sample 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找最大流

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxn 10001 #define INF 19260817 #define mem(a) memset(a,0,sizeof(a)) using namespace std; long long int cnt,cost[maxn],from[maxn],to[maxn],Next[maxn],head[maxn]; int level[maxn];//層數 queue<int>q; int S,T,n,m; void add(int x,int y,int z){ //建邊++cnt;cost[cnt]=z;from[cnt]=x;to[cnt]=y;Next[cnt]=head[x];head[x]=cnt; } bool bfs(){ //bfs分層&&判斷是否還有增廣路 memset(level,-1,sizeof(level));level[S]=0;q.push(S);while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=Next[i]){int v=to[i];if(cost[i]!=0&&level[v]==-1){ //如果容量是0||已被更新 就不更新了level[v]=level[u]+1;//更新層數 q.push(v);}}}if(level[T]!=-1)return true; //如果流不動了就結束dinicreturn false; } int dfs(int u,int flow){ //dfs找最大流if(u==T)return flow;long long int ret=flow; //記錄初始流量for(int i=head[u];i!=-1;i=Next[i]){if(ret<=0)break; //如果已經沒流了就退出int v=to[i];if(cost[i]!=0&&level[u]+1==level[v]){int k=dfs(v,min(cost[i],ret)); //把能流的都給下一個點ret-=k;cost[i]-=k;cost[i^1]+=k; //邊權更新,剩余流量更新}}return flow-ret; //返回流出的流量(總流量-剩余流量) } int dinic(){int ans=0;while(bfs()==true){ans+=dfs(S,INF); //累加最大流}return ans; } void init() {mem(cost);mem(from);mem(to);mem(Next);mem(level);} int main(){while( ~scanf("%d%d",&m,&n)){init();初始化 cnt=1;memset(head,-1,sizeof(head));if(!q.empty()){q.pop();}S=1;//源點 T=n;//始點 for(int i=1;i<=m;i++){int x,y,z;scanf("%d%d%d",&x,&y,&z);add(x,y,z);add(y,x,0); //EK一樣建邊}printf("%d\n",dinic());}} //dinic時間復雜度:O(n^2 m).

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。