POJ---1273 Drainage Ditches【最大流】

来源:互联网 发布:linux mint18美化 编辑:程序博客网 时间:2024/06/15 17:48

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

Sample Output

50

SOURCE:点击打开链接


题意:

   赤裸裸的网络流题目。给定点数,边数,每条边的容量,以及源点,汇点,求最大流。

解析:

   通过Ford-Fulkerson算法或者是Dinic算法,通过dfs搜索最小流进行增广路。

Ford-Fulkerson算法:

#include <iostream>#include <cstring>#include <algorithm>#define MAX 205#define INF 0x7fffffffusing namespace std;int n,m;int c[MAX][MAX];bool pass[MAX];int dfs(int s,int t,int low);//s为源点,t为汇点,low为当前流int dfs_max_flow(int s,int t);int main(void){    int a,b,d;    while (cin>>n>>m)    {        memset(c,0,sizeof(c));        for (int i=1; i<=n; ++i)        {            cin>>a>>b>>d;            c[a][b]+=d;        }        cout<<dfs_max_flow(1,m)<<endl;    }    return 0;}int dfs(int s,int t,int low){    if (s==t)        return low;    pass[s]=1;    for (int i=1; i<=m; ++i)    {        if (!pass[i]&&c[s][i])        {            int flow=dfs(i,t,min(low,c[s][i]));            if(flow>0)  //增广该路径            {                c[s][i]-=flow;                c[i][s]+=flow;                return flow;            }        }    }    return 0;}int dfs_max_flow(int s,int t){    memset(pass,0,sizeof(pass));    int maxflow=0,flow;    while (flow=dfs(s,t,INF)) //不断地增广路径进行搜索,直到无法继续增加才结束    {        memset(pass,0,sizeof(pass));        maxflow+=flow;    }    return maxflow;}

Dinic算法:

#include<iostream>#include<cstring>#include<queue>#define maxn 205#define INF 0x7fffffffusing namespace std;int n, m;int c[maxn][maxn];int dis[maxn];//dis[i]表示到原点s的层数bool bfs();int  dfs(const int x,const int low);int main(void){    int s,t,d;    while(cin>>n>>m)    {        memset(c,0,sizeof(c));        for(int i = 0  ; i < m; i++)        {            cin>>s>>t>>d;            c[s][t] += d;        }        int ans = 0;        int res;        while(bfs())            while(res = dfs(1,INF))                ans+= res;        cout<<ans<<endl;    }    return 0;}bool bfs()// 重新建图按层数建图{    memset(dis,-1,sizeof(dis));    int k;    dis[1] = 0;    queue<int> que;    que.push(1);    while(que.size())    {        k = que.front();        que.pop() ;        for( int i = 1; i<= m; i++)            if(c[k][i] > 0 && dis[i] < 0 )// 如果 可以  可以到达 但 还没有 访问            {                dis[i] = dis[k]+ 1;                que.push(i);            }    }    if(dis[n] > 0)        return true;    else        return false;}int  dfs(const int x,const int low)// 查找路径上的最小的流量{    int flow;    if(x == n)        return low;    for(int i = 1; i<= n; i++)        if(c[x][i] > 0 && dis[i] == dis[x] + 1)        {            flow=dfs(i,min(low,c[x][i]));            if(flow>0)            {                c[x][i] -= flow;                c[i][x] += flow;                return flow;            }        }    return 0;}



原创粉丝点击