题目:http://poj.org/problem?id=3469


题意:有双核处理器,有n个任务,给出每个任务在分别在两个处理核心上工作的花费,然后有m行,每行给出两个任务,如果两个任务不在同一个处理核心上工作,那么将有额外的花费。求最小花费


思路:最小割。之前用dinic算法做的,加上当前弧优化6000ms。省赛的时候yjj看到我的最大流板子是 dinic,说到你怎么用这个板子,很容易被卡,学点更快的算法吧。于是回来就学了SAP算法,加当前弧和gap优化,果然更快,2800ms。贴个板子,以备后用

模板一:

这个板子稍微快了一点点。。。但是代码长了很多,因为多了一个bfs函数

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;

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

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++;
}
void bfs(int t) //从汇点出发反向搜索给为每个点确定距离标号,即每个点到汇点需要经过的最少边数,gap数组储存相同标号的点数
{
    memset(level, -1, sizeof level);
    memset(gap, 0, sizeof gap);
    int st = 0, en = 0;
    level[t] = 0;
    que[en++] = t;
    gap[level[t]]++;
    while(st < en)
    {
        int v = que[st++];
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(level[u] < 0)
            {
                level[u] = level[v] + 1;
                gap[level[u]]++;
                que[en++] = u;
            }
        }
    }
}
int sap(int s, int t)
{
    bfs(t);
    memcpy(cur, head, sizeof head);
    //gap[0] = nv;
    int v = pre[s] = s, flow = 0, aug = INF;
    while(level[s] < nv)
    {
        bool flag = false;
        for(int &i = cur[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[v] == level[u] + 1)
            {
                flag = true;
                pre[u] = v;
                v = u;
                aug = min(aug, g[i].cap);
                if(v == t) //找到一条增广路
                {
                    flow += aug;
                    while(v != s) //路径回溯更新残留网络
                    {
                        v = pre[v];
                        g[cur[v]].cap -= aug;
                        g[cur[v]^1].cap += aug;
                    }
                    aug = INF;
                }
                break;
            }
        }
        if(flag) continue;
        int minlevel = nv;
        for(int i = head[v]; i != -1; i = g[i].next) //寻找与当前点相连接的点中最小的距离标号
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[u] < minlevel)
                minlevel = level[u], cur[v] = i; //保存弧
        }
        if(--gap[level[v]] == 0) break; //更新gap数组后如果出现断层,则源点汇点之间必定无流,直接退出
        level[v] = minlevel + 1; //更新距离标号
        gap[level[v]]++;
        v = pre[v]; //转当前点的前驱节点继续寻找允许弧
    }
    return flow;
}
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);
        }
        nv = n + 2;
        printf("%d\n", sap(0, n + 1));
    }
	return 0;
}


模板二:

没有bfs函数,初始所有点的距离标号为0,在增广过程中维护距离标号


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

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

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++;
}
int sap(int s, int t)
{
    memset(level, 0, sizeof level);
    memset(gap, 0, sizeof gap);
    memcpy(cur, head, sizeof head);
    gap[0] = nv; //初始距离标号均为0,所以gap[0] = nv,即所有点
    int v = pre[s] = s, flow = 0, aug = INF;
    while(level[s] < nv)
    {
        bool flag = false;
        for(int &i = cur[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[v] == level[u] + 1)
            {
                flag = true;
                pre[u] = v;
                v = u;
                aug = min(aug, g[i].cap);
                if(v == t)
                {
                    flow += aug;
                    while(v != s)
                    {
                        v = pre[v];
                        g[cur[v]].cap -= aug;
                        g[cur[v]^1].cap += aug;
                    }
                    aug = INF;
                }
                break;
            }
        }
        if(flag) continue;
        int minlevel = nv;
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[u] < minlevel)
                minlevel = level[u], cur[v] = i;
        }
        if(--gap[level[v]] == 0) break;
        level[v] = minlevel + 1;
        gap[level[v]]++;
        v = pre[v];
    }
    return flow;
}
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);
        }
        nv = n + 2;
        printf("%d\n", sap(0, n + 1));
    }
	return 0;
}