【网络流 最大流模板题Dinic EK】POJ - 1273 P - Drainage Ditches

P - Drainage Ditches  POJ - 1273 

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

EK算法

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define inf 1<<29
#define CL(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=205;
int n,m,s,e,u,v,cost;
int Map[N][N],path[N],flow[N];
queue <int> q;

int bfs()
{
    while(!q.empty()) q.pop();
    CL(path,-1);
    path[s]=0,flow[s]=inf;
    q.push(s);
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        if(t==e) break;
        for(int i=1;i<=m;i++)
        {
            if(i!=s&&path[i]==-1&&Map[t][i])
            {
                flow[i]=min(flow[t],Map[t][i]);
                q.push(i);
                path[i]=t;
            }
        }
    }
    if(path[e]==-1) return -1;
    return flow[e];
}

int Edmonds_Karp()
{
    int max_flow=0,step,now,pre;
    while((step=bfs())!=-1)
    {
        max_flow+=step;
        now=e;
        while(now!=s)
        {
            pre=path[now];
            Map[pre][now]-=step;
            Map[now][pre]+=step;
            now=pre;
        }
    }
    return max_flow;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        CL(Map,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&u,&v,&cost);
            Map[u][v]+=cost;
        }
        s=1,e=m;
        printf("%d\n",Edmonds_Karp());
    }
    return 0;
}

 Dinic算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAX_V=205;
const int INF=0x3f3f3f3f;
struct edge
{
    int to,cap,rev;
};

vector <edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue <int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty())
    {
        int v=que.front();que.pop();
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)
{
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        bfs(s); //计算层次图
        if(level[t]<0) return flow; //找不到s-t路径
        memset(iter,0,sizeof(iter)); //初始化当前弧
        int f;
        while((f=dfs(s,t,INF))>0)  //更新最大流
            flow+=f;
    }
    return flow;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        for(int i=0;i<=n;i++) G[i].clear();
        for(int i=1;i<=m;i++)
        {
            int from,to,cap;
            scanf("%d%d%d",&from,&to,&cap);
            add_edge(from,to,cap);
        }
        printf("%d\n",max_flow(1,n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81486152