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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

最大流自用模板(例题:HDU1532)

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最大流自用模板(例题:HDU1532) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

三種模板:Edmonds_Karp,Dinic,SAP

例題:

Drainage Ditches(HDU1532)

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22365????Accepted Submission(s): 10683)

?

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 4

1 2 40

1 4 20

2 4 20

2 3 30

3 4 10

?

Sample Output

50?

?

Source

HDU1532

題意:最大流模板題

方法一:Edmonds_Karp
?

#include <cstdio> #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> const int maxn=205; const int INF=0x3f3f3f3f; using namespace std;struct Edge {int from,to,cap,flow;Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){} };vector<Edge> edges; vector<int> G[maxn]; int a[maxn]; int pre[maxn];void init(int n) {for (int i=0;i<n;i++)G[i].clear();edges.clear(); }void addedge(int from,int to,int cap) {edges.push_back(Edge(from,to,cap,0));edges.push_back(Edge(to,from,0,0));int m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1); }int Edmonds_Karp(int s,int t) {int flow=0;while(1){memset(a,0,sizeof(a));queue<int> Q;Q.push(s);a[s]=INF;while(!Q.empty()){int x=Q.front();Q.pop();int sz=G[x].size();for (int i=0;i<sz;i++){Edge& e=edges[G[x][i]];if (!a[e.to]&&e.cap>e.flow){pre[e.to]=G[x][i];a[e.to]=min(a[x],e.cap-e.flow);Q.push(e.to);}}if (a[t])break;}if (!a[t])break;for (int u=t;u!=s;u=edges[pre[u]].from){edges[pre[u]].flow+=a[t];edges[pre[u]^1].flow-=a[t];}flow+=a[t];}return flow; }int main() {int n,m,u,v,f;while(cin >> n >> m){init(n);for (int i=0;i<n;i++){scanf("%d%d%d",&u,&v,&f);addedge(u,v,f);}cout << Edmonds_Karp(1,m) << endl;}return 0; }

方法二:Dinic

#include <cstdio> #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> const int maxn=205; const int INF=0x3f3f3f3f; using namespace std;struct Edge {int from,to,flow;Edge(int u,int v,int f):from(u),to(v),flow(f){} };vector<Edge> edges; vector<int> G[maxn];int dis[maxn]; int cur[maxn];void init(int n) {for (int i=0;i<n;i++)G[i].clear();edges.clear(); }void addedge(int from,int to,int flow) {edges.push_back(Edge(from,to,flow));edges.push_back(Edge(to,from,0));int m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1); }bool bfs(int s,int t) {queue<int> Q;Q.push(s);memset(dis,-1,sizeof(dis));dis[s]=0;while(!Q.empty()){int x=Q.front();Q.pop();int sz=G[x].size();for (int i=0;i<sz;i++) {Edge& e=edges[G[x][i]];if (e.flow>0) {if (dis[e.to]<0) {dis[e.to]=dis[x]+1;Q.push(e.to);}}}}return bool(~dis[t]); }int dfs(int s,int t,int maxflow) {if (s==t)return maxflow;int sz=int(G[s].size());for (int i=cur[s],num;num=G[s][i],i<sz;i++){cur[s]=i;Edge &e=edges[num];if (dis[e.to]==dis[s]+1 && e.flow>0){int flow=dfs(e.to,t,min(maxflow,e.flow));if (flow!=0){e.flow-=flow;edges[num^1].flow+=flow;return flow;}}}return 0; }int Dinic(int s,int t) {int ans=0;while(bfs(s,t)){int flow;memset(cur,0,sizeof(cur));while((bool)(flow=dfs(s,t,INF)))ans+=flow;}return ans; }int main() {int n,m,u,v,f;while(cin >> n >> m){init(n);for (int i=0;i<n;i++){scanf("%d%d%d",&u,&v,&f);addedge(u,v,f);}cout << Dinic(1,m) << endl;}return 0; }

方法三:SAP

#include <cstdio> #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> const int maxn=205; const int maxm=maxn*maxn; const int INF=0x3f3f3f3f; using namespace std;struct Edge { int v,w,next; }edge[maxm];int dis[maxn],pre[maxn],rec[maxn],head[maxn],gap[maxn],now[maxn]; int n,m,no,up; queue<int> q;void addedge(int u,int v,int w) {edge[no].v=v; edge[no].w=w;edge[no].next=head[u]; head[u]=no++;edge[no].v=u; edge[no].w=0;edge[no].next=head[v]; head[v]=no++; }void pre_init() { no=0; up=n;memset(head,-1,sizeof(head)); }void init(int s,int t) {for(int i=0;i<=up;i++) {now[i]=head[i];gap[i]=0;dis[i]=INF;}while(!q.empty())q.pop();dis[t]=0; q.push(t);while(!q.empty()){int tp=q.front();q.pop();gap[dis[tp]]++;int k=head[tp];while(k!=-1){if(dis[edge[k].v]==INF && edge[k^1].w) {dis[edge[k].v]=dis[tp]+1; q.push(edge[k].v); }k=edge[k].next; }} }int SAP(int s,int t) { int ans=0,flow=INF,top=s; pre[s]=s;init(s,t); while(dis[s]<up) { if(top==t) { ans+=flow; while(top!=s){ edge[rec[top]].w-=flow; edge[rec[top]^1].w+=flow; top=pre[top]; }flow=INF; } int k=now[top]; while(k!=-1) { int v=edge[k].v; if(edge[k].w && dis[top]==dis[v]+1) { flow=min(flow, edge[k].w); pre[v]=top; rec[v]=k; now[top]=k; top=v; break; } k=edge[k].next; } if(k==-1){ int mind=n; if(--gap[dis[top]]==0) break;int k=now[top]=head[top];while(k!=-1) { if(edge[k].w && mind>dis[edge[k].v])mind=dis[edge[k].v]; k=edge[k].next; } gap[dis[top]=mind+1]++; top=pre[top]; }}return ans; }int main() {int u,v,f;while(cin >> n >> m){pre_init();for (int i=0;i<n;i++){scanf("%d%d%d",&u,&v,&f);addedge(u,v,f);}cout << SAP(1,m) << endl;}return 0; }

?

轉載于:https://www.cnblogs.com/Radium1209/p/10415348.html

總結

以上是生活随笔為你收集整理的最大流自用模板(例题:HDU1532)的全部內容,希望文章能夠幫你解決所遇到的問題。

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