最大流自用模板(例题: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
?
方法二: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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AE滤镜初始学习
- 下一篇: nodejs之express入门