dinic模板 当前弧+多路增广

题目:

http://poj.org/problem?id=3469
实测发现,当前弧、多路增广和当前弧+多路增广这三种优化其实差不多。。。但还是留个模板吧

只有当前弧:

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <queue>  
using namespace std;  

const int N = 20010;  
const int INF = 0x3f3f3f3f;  
struct edge  
{  
    int to, cap, next;  
} g[N*50];  
int head[N], iter[N], level[N];  
int cnt;  
void add_edge(int v, int u, int cap)  
{  
    g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;  
    g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;  
}  
bool bfs(int s, int t)  
{  
    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 = head[v]; i != -1; i = g[i].next)  
        {  
            int u = g[i].to;  
            if(g[i].cap > 0 && level[u] < 0)  
            {  
                level[u] = level[v] + 1;  
                que.push(u);  
            }  
        }  
    }  
    return level[t] == -1;  
}  
int dfs(int v, int t, int f)  
{  
    if(v == t) return f;  
    for(int &i = iter[v]; i != -1; i = g[i].next)  
    {  
        int u = g[i].to;  
        if(g[i].cap > 0 && level[v] < level[u])  
        {  
            int d = dfs(u, t, min(f, g[i].cap));  
            if(d > 0)  
            {  
                g[i].cap -= d, g[i^1].cap += d;  
                return d;  
            }  
        }  
    }  
    return 0;  
}  
int dinic(int s, int t)  
{  
    int flow = 0, f;  
    while(true)  
    {  
        if(bfs(s, t)) return flow;  
        memcpy(iter, head, sizeof head);  
        while(f = dfs(s, t, INF), f > 0)  
            flow += f;  
    }  
}  
int main()  
{  
    int n, m, a, b, c;  
    while(~ scanf("%d%d", &n, &m))  
    {  
        cnt = 0;  
        memset(head, -1, sizeof head);  
        for(int i = 1; i <= n; i++)  
        {  
            scanf("%d%d", &a, &b);  
            add_edge(0, i, a);  
            add_edge(i, n + 1, b);  
        }  
        for(int i = 0; i < m; i++)  
        {  
            scanf("%d%d%d", &a, &b, &c);  
            add_edge(a, b, c);  
            add_edge(b, a, c);  
        }  
        printf("%d\n", dinic(0, n + 1));  
    }  
    return 0;  
}

当前弧+多路增广:

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#include <queue>
using namespace std;

const int N = 20100;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to, cap, next;
}g[N*100];
int level[N], iter[N], head[N], que[N];
int n, m, cnt;

void add_edge(int v, int u, int cap)
{
    g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
    g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
bool bfs(int s, int t)
{
    memset(level, -1, sizeof level);
    level[s] = 0;
    int st = 0, en = 0;
    que[0] = s;
    while(st <= en)
    {
        int v = que[st++];
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[u] < 0)
            {
                level[u] = level[v] + 1;
                que[++en] = u;
            }
        }
    }
    return level[t] == -1;
}
//下面两个dfs都是当前弧+多路增广
int dfs(int v, int t, int f)
{
    if(v == t) return f;
    int num = 0;
    for(int &i = iter[v]; i != -1; i = g[i].next)
    {
        int u = g[i].to;
        if(g[i].cap > 0 && level[v] < level[u])
        {
            int d = dfs(u, t, min(f - num, g[i].cap));
            g[i].cap -= d, g[i^1].cap += d, num += d;
            if(num >= f) break;
        }
    }
    if(! num) level[v] = -1;
    return num;
}
int dfs(int v, int t, int f)
{
    if(v == t) return f;
    int tm = f;
    for(int &i = iter[v]; i != -1; i = g[i].next)
    {
        int u = g[i].to;
        if(g[i].cap > 0 && level[v] < level[u])
        {
            int d = dfs(u, t, min(tm, g[i].cap));
            g[i].cap -= d, g[i^1].cap += d, tm -= d;
            if(tm == 0) break;
        }
    }
    if(tm == f) level[v] = -1;
    return f - tm;
}
int dinic(int s, int t)
{
    int flow = 0, f;
    while(true)
    {
        if(bfs(s, t)) return flow;
        memcpy(iter, head, sizeof head);
        flow += dfs(s, t, INF);
    }
}
int main()
{
    int a, b, c;
    while(~ scanf("%d%d", &n, &m))
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &a, &b);
            add_edge(0, i, a);
            add_edge(i, n + 1, b);
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            add_edge(a, b, c);
            add_edge(b, a, c);
        }
        printf("%d\n", dinic(0, n + 1));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值