HDU1532 Drainage Ditches (maxflow

    xiaoxiao2025-09-06  629

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15232 Accepted Submission(s): 7237

    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

    这道题我用来练习一下Dinic的使用,效果还是不错的。

    #include<iostream> using namespace std; #include<cstdio> #include<queue> #include<algorithm> #include<cstring> /** Dinic算法的实现有以下几个步骤: 1:初始化容量网络和网络流 2:构造残留网络和层次网络,若汇点不在层次网络中则算法结束 输出最大流 3:在层次网络中用一次DFS进行增广,DFS执行完毕,该阶段的增广也就完毕了。 4:转至步骤2 Hint: 1、注意que的大小,不确定尽量开大 2、注意流量的大小,此处为int */ const int inf=0x3f3f3f3f; const int maxn=205; const int maxm=1e5+100; int top; int head[maxn]; int n,m,to; int level[maxn]; bool used[maxn]; struct note{ int to,next,flow; }edge[maxm]; int que[maxm]; bool dinic_bfs(int s,int t) { //构造层次网络 memset(level,0,sizeof(level));//初始化顶点的层次 为0 memset(used,false,sizeof(used)); int l=0,r=0; que[r++]=s; level[s]=1; int now; used[s]=true; while(l<r) { now=que[l++]; used[now]=false; for(int i=head[now];~i;i=edge[i].next){ to=edge[i].to; if((!level[to])&&(edge[i].flow>0)){ level[to]=level[now]+1; if(!used[to]){ used[to]=true,que[r++]=to; } } } } return level[t]!=0;//若返回false表明 汇点不在层次网络中 } int dfs(int now,int t,int nowf) { //进行增广 int nowflow=nowf; if(now==t) return nowflow; for(int i=head[now];~i;i=edge[i].next){ to=edge[i].to; if((level[to]==level[now]+1)&&(edge[i].flow>0)){ int getflow=dfs(to,t,min(edge[i].flow,nowflow)); edge[i].flow-=getflow; edge[i^1].flow+=getflow; nowflow-=getflow; } } return nowf-nowflow; } int Dinic(int s,int t) { int maxflow=0,nowflow; while(dinic_bfs(s,t)) { while(nowflow=dfs(s,t,inf)) { maxflow+=nowflow; } } return maxflow; } inline void ADD(int u,int v,int f){ edge[top].next=head[u]; edge[top].to=v; edge[top].flow=f; head[u]=top++; edge[top].next=head[v]; edge[top].to=u; edge[top].flow=0; head[v]=top++; } int main() { int u,v,f; while(~scanf("%d%d",&n,&m)) { memset(head,-1,sizeof(head)),top=0; for(int i=0;i<n;++i){ scanf("%d%d%d",&u,&v,&f); ADD(u,v,f); } printf("%d\n",Dinic(1,m)); } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1302368.html
    最新回复(0)