sap 模板

本文详细介绍了 SAP (Shortest Augmenting Path) 算法在解决最大流问题中的应用,并通过 HDU4280 问题进行实战演示。文章包含完整的 C 语言实现代码,展示了如何构建图模型、执行增广路径查找以及更新残余网络等关键步骤。
摘要由CSDN通过智能技术生成
// hdu 4280
#include <stdio.h>
#define MAXV 100010
#define MAXE 400010
#define INF 0x7fffffff;
struct Node
{
    int u, v, next;
    int cap;
}edge[MAXE];
int head[MAXV],en;

void add_edge(int u, int v, int cap )
{
    edge[en].u = u;
    edge[en].v = v;
    edge[en].cap = cap;
    edge[en].next = head[u];
    head[u] = en++;

    edge[en].u = v;
    edge[en].v = u;
    edge[en].cap = 0;
    edge[en].next = head[v];
    head[v] = en++;
}

void get_map( int n, int m )
{
    int i,u,v,w;
    en = 0;
    for(i=0;i<=n;i++ ) head[i]=-1;
    for( i=0; i<m; i ++ )
    {
        scanf("%d%d%d", &u, &v, &w);
        add_edge(u,v,w);
        add_edge(v,u,w);
    }
}

int que[MAXE], gap[MAXV], height[MAXV], cur[MAXV], sta[MAXV];
void sap_bfs( int t )
{
    int front = 0, rear = 0,u,v, p;
    gap[0] = 1;
    height[t] = 0;
    que[rear++] = t;

    while (front != rear)
    {
        u = que[front++]; front = front==MAXE?0:front;
        for( p=head[u]; p!=-1; p=edge[p].next )
        if ( edge[p].cap == 0 && height[edge[p].v] == -1)
        {
            que[rear++] = edge[p].v; rear = rear==MAXE?0:rear;
            ++gap[ height[edge[p].v]=height[u]+1 ];
        }
    }
}

int sap( int s, int t, int n)
{
    int max_flow = 0, top = 0, u = s, i, tmp, p, mx ;
    for( i=0;i<=n+1;i++)
    {
        cur[i]=head[i];
        height[i]=-1;
        gap[i]=0;
    }
    sap_bfs(t);
    while ( height[s] < n )
    {
        if (u == t )
        {
            tmp = INF; p = n;
            for (i=0; i!=top; ++i)
                if (tmp > edge[sta[i]].cap)
                {
                    tmp = edge[sta[i]].cap;
                    p = i;
                }
            for (i=0; i!=top; ++i)
            {
                edge[sta[i]].cap -= tmp;
                edge[sta[i]^1].cap += tmp;
            }
            max_flow += tmp;
            top = p;
            u = edge[sta[top]].u;
        }

        if (u != t && gap[height[u] -1] == 0)
            break;
        for (i = cur[u]; i != -1; i = edge[i].next)
            if (edge[i].cap != 0 && height[u] == height[edge[i].v] + 1)
                break;

        if (i != -1)
        {
            cur[u] = i;
            sta[top++] = i;
            u = edge[i].v;
        }
        else
        {
            mx = n;
            for (i = head[u]; i != -1; i = edge[i].next)
            {
                if (edge[i].cap != 0 && mx > height[edge[i].v])
                {
                    mx = height[edge[i].v];
                    cur[u] = i;
                }
            }
            --gap[height[u]];
            ++gap[height[u] = mx + 1];
            if (u != s )
                u = edge[sta[--top]].u;
        }
    }
    return max_flow;
}

int main()
{
    int cas, n,m,x,y,s,t,ts,tt,i;
    scanf("%d", &cas);
    while( cas -- )
    {
        scanf("%d%d",&n,&m);
        ts = INF;
        tt = -INF;
        for(i=1; i<=n; i++ )
        {
            scanf("%d%d", &x, &y);
            if (x < ts ) s = i,  ts = x;
            if (x > tt ) t = i, tt = x;
        }
        get_map(n,m);
        x = sap(s,t,n);
        printf("%d\n", x);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值