POJ 1273 Drainage Ditches(网络流入门)

来源:互联网 发布:大连网络教育 编辑:程序博客网 时间:2024/06/05 21:51

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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50

 题意:

      每次下雨的时候农场里总会积满水,农场主想修建一套排水沟去排水,现在知道每条沟的排水量,问从起点把水全部排放到池塘的最大流量。

思路:

     题上说的很明白就是让你求一个最大流,网络流的题建图是关键,这题给的很明显就不用考虑建图的事,可以直接套用Dinic算法 。

Dinic算法的大致可以分成几块:

1.构建层次网络;

2.找到一条可以到Vt的路

3.在找到的可行路上找出最小边容量(minc)

4.修改容量网络

5.退栈。

基本上每一步都对应了一个循环,这样写下来大致的思路就有了。

code:

#include<stdio.h>#include<string.h>#include<queue>#include<deque>#include<algorithm>using namespace std;#define INF 0x3f3f3f3f#define MAXN 210int vis[MAXN],sign[MAXN];int G[MAXN][MAXN],n,m;int Countsign()//构造层次网络{    queue<int>Q;    memset(sign,-1,sizeof(sign));//所有点的层号都初始化为-1    sign[1]=0;//源点层为0    Q.push(1);//源点入队    while(!Q.empty())    {        int v=Q.front();        Q.pop();        for(int i=1; i<=n; i++)        {            if(G[v][i]>0&&sign[i]==-1)            {//G[v][i]>0 说明v->i之间有路             //sign[i]==-1 说明i这个点还没有访问过                sign[i]=sign[v]+1; //v->i i的层号等于v+1;                Q.push(i);            }        }    }    //sign[n]==-1 说明汇点没有被访问到,故不能分层成功    if(sign[n]==-1) return 0;    return 1;}int Dinic(){    int i,s,maxflow=0;    while(Countsign())//只要能分层    {        deque<int>Q;//dfs用的栈        Q.push_back(1);//源点入栈        memset(vis,0,sizeof(vis));        vis[1]=1;        while(!Q.empty())        {            int nd=Q.back();            if(nd==n)            {//如果nd是汇点                int minc=INF;//这一条可行路上最小流量                int minvs;//容量最小边的起点                for(i=1; i<Q.size(); i++)                {                    int vs=Q[i-1];                    int ve=Q[i];                    if(G[vs][ve]>0)                    {                        if(minc>G[vs][ve])//找可行路上最小流量                        {                            minc=G[vs][ve];                            minvs=vs;                        }                    }                }                maxflow+=minc;                for(i=1; i<Q.size(); i++)                {                    int vs=Q[i-1];                    int ve=Q[i];                    G[vs][ve]-=minc;//修改边容量                    G[ve][vs]+=minc;//添加反向边                }                //退栈到minvs成为栈顶,以便继续dfs                while(!Q.empty()&&Q.back()!=minvs)                {                    vis[Q.back()]=0;                    Q.pop_back();                }            }            else            {//nd不是汇点                for(i=1; i<=n; i++)                {                    if(G[nd][i]>0&&sign[i]==sign[nd]+1&&!vis[i])                    { //往下一层没有走过的节点走                        vis[i]=1;                        Q.push_back(i);                        break;                    }                }                //如果找不到下一个可以走的点                if(i>n)  Q.pop_back();//回溯            }        }    }    return maxflow; //返回该网络的最大流}int main(){    while(~scanf("%d%d",&m,&n))    {        int u,v,c;        memset(G,0,sizeof(G));        for(int i=0; i<m; i++)        {            scanf("%d%d%d",&u,&v,&c);//u->v容量为c            G[u][v]+=c;//两点之间可能有多条边        }        printf("%d\n",Dinic());    }    return 0;}

阅读全文
1 0
原创粉丝点击