网络流SAP+gap+弧优化算法

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 54962 Accepted: 20960

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
#include"stdio.h"
#include"string.h"
#include"queue"
#include"stack"
#include"iostream"
#include"stdlib.h"
#define inf 99999999
#define M 50000
using namespace std;
struct st
{
    int u,v,w,next;
}edge[M];
int t,head[M],cur[M],q[M],gap[M],dis[M];
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].w=w;
    edge[t].next=head[u];
    head[u]=t++;

    edge[t].u=v;
    edge[t].v=u;
    edge[t].w=0;
    edge[t].next=head[v];
    head[v]=t++;
}
void bfs(int start,int endl)//建立到汇点的距离层次图存在dis[]数组中
{
    int rear=0,i,j;
    memset(dis,-1,sizeof(dis));
    memset(gap,0,sizeof(gap));//gap[x]记录dis[i]=x出现了多少次
    dis[endl]=0;
    gap[dis[endl]]=1;
    q[rear++]=endl;
    for(i=0;i<rear;i++)
    {
        for(j=head[q[i]];j!=-1;j=edge[j].next)
        {
            int v=edge[j].v;
            if(dis[v]==-1)
            {
                ++gap[dis[v]=dis[q[i]]+1];
                q[rear++]=v;
            }
        }
    }
}
int SAP(int start,int endl,int n)
{
    int ans=0;
    bfs(start,endl);
    int cur[M];//代替head数组
    memcpy(cur,head,sizeof(head));
    int stack[M],top=0;//建立手工栈
    int u=start,i;
    while(dis[start]<n)
    {
        if(u==endl)//当搜到终点时即找到从原点到汇点的增光路,正常处理即可
        {
            int mini=inf,tep;
            for(i=0;i<top;i++)
            {
                if(mini>edge[stack[i]].w)
                {
                    mini=edge[stack[i]].w;
                    tep=i;
                }
            }
            for(i=0;i<top;i++)
            {
                edge[stack[i]].w-=mini;
                edge[stack[i]^1].w+=mini;
            }
            ans+=mini;
            top=tep;
            u=edge[stack[top]].u;//此时的u为变容量为0的u
        }
        if(dis[u]&&gap[dis[u]-1]==0)//出现了断层,没有增广路
            break;
        for(i=cur[u];i!=-1;i=edge[i].next)//遍历与u相连的未遍历的节点
        {
            int v=edge[i].v;
            if(dis[v]!=-1)
            {
                if(edge[i].w&&dis[u]==dis[v]+1)//层次关系找到允许路径
                    break;
            }
        }
        if(i!=-1)//找到允许弧
        {
            cur[u]=i;
            stack[top++]=i;
            u=edge[i].v;
        }
        else//无允许的路径,修改标号 当前点的标号比与之相连的点中最小的多1
        {
            int mini=n;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                if(edge[i].w==0)continue;
                int v=edge[i].v;
                if(mini>dis[v])//找到与u相连的v中dep[v]最小的点
                {
                    mini=dis[v];
                    cur[u]=i;//最小标号就是最新的允许弧
                }
            }
            --gap[dis[u]];//dep[u] 的个数变化了 所以修改gap
            ++gap[dis[u]=mini+1];//将dep[u]设为min(dep[v]) + 1, 同时修改相应的gap[]
            if(u!=start)//该点非源点&&以u开始的允许弧不存在,退点
                u=edge[stack[--top]].u;
        }
    }
    return ans;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&m,&n)!=-1)
    {
        init();
        while(m--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        int ans=SAP(1,n,n);
        printf("%d\n",ans);
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值