HDU 5520 (费用流)

题目链接:点击这里

参考自这里
题意:给出一个网格图,要把奇数和偶数连起来,然后所有剩下的空格子连城若干个环(两个点可以重复走构成一个环)。已知每一对格子之间连边的花费,求最小费用。

把每一个格子拆成入点和出点,然后这样建模:
1. S到奇数入点建边,流量为1费用为0;
2. 偶数出点到T建边,流量为1费用为0;
3. S到空点入点建边,空点出点到T建边,流量为1费用为0;
4. 每一个点的入点到上下左右的点的出点建边,流量为1费用为连边费用。
这样跑一次费用流判断是否满流即可。因为这样的最大流必然是空格子参与某一条奇偶路径或者环。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
#define maxn 5005
#define maxm 1000005
#define type long long
#define INF 1e15

int n, m;
int s, t;
struct node {
    int u, v, next;
    type cap, flow, cost;
}edge[maxm];
int head[maxn], cnt;
int pre[maxn];
type dis[maxn];
bool vis[maxn];
int N;

void init () {
    memset (head, -1, (n*m*2+5)*sizeof head[0]);
    cnt = 0;
}

void add_edge (int u, int v, type cap, type cost) {
    edge[cnt].u = u, edge[cnt].v = v, edge[cnt].cap = cap, edge[cnt].flow = 0;
    edge[cnt].cost = cost, edge[cnt].next = head[u], head[u] = cnt++;
    edge[cnt].u = v, edge[cnt].v = u, edge[cnt].cap = 0, edge[cnt].flow = 0;
    edge[cnt].cost = -cost, edge[cnt].next = head[v], head[v] = cnt++;
}

bool spfa (int s, int t) {
    queue <int> q;
    for (int i = 0; i < N; i++) {
        dis[i] = INF;
        vis[i] = 0;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = 1;
    q.push (s);
    while (!q.empty ()) {
        int u = q.front (); q.pop ();
        vis[u] = 0;
        for (int i = head[u]; i != -1; i = edge[i].next) {
            int v = edge[i].v;
            if (edge[i].cap > edge[i].flow && dis[v] > dis[u]+edge[i].cost) {
                dis[v] = dis[u]+edge[i].cost;
                pre[v] = i;
                if (!vis[v]) {
                    vis[v] = 1;
                    q.push (v);
                }
            }
        }
    }
    if (pre[t] == -1)
        return 0;
    else
        return 1;
}

int MCMF (int s, int t, type &cost) {
    type flow = 0;
    cost = 0;
    while (spfa (s, t)) {
        type Min = INF;
        for (int i = pre[t]; i != -1; i = pre[edge[i^1].v]) {
            if (Min > edge[i].cap-edge[i].flow) {
                Min = edge[i].cap-edge[i].flow;
            }
        }
        for (int i = pre[t]; i != -1; i = pre[edge[i^1].v]) {
            edge[i].flow += Min;
            edge[i^1].flow -= Min;
            cost += edge[i].cost*Min;
        }
        flow += Min;
    }
    return flow;
}

int get (int x, int y) {
    return x*m+y;
}

int getint () {
    bool s=0;
    int a,f;
    while(a=getchar(),!(a>='0'&&a<='9')&&a!='-');
    if(a-'-')
        a-='0';
    else
        a=0,s=1;
    while(f=getchar(),f>='0'&&f<='9')
        a=a*10+f-'0';
    return s ? -a:a;
}

int main () {
    int tt, kase = 0, maxflow;
    scanf ("%d", &tt);
    while (tt--) {
        printf ("Case #%d: ", ++kase);
        n = getint (), m = getint ();
        maxflow = 0;
        init ();
        N = 2*n*m+2, s = N-2, t = N-1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int num;
                num = getint ();
                if (!num) {
                    add_edge (s, get (i, j), 1, 0);
                    add_edge (get (i, j)+n*m, t, 1, 0);
                    maxflow++;
                }
                else if (num&1) {
                    add_edge (s, get (i, j), 1, 0);
                    maxflow++;
                }
                else {
                    add_edge (get (i, j)+n*m, t, 1, 0);
                }
            }
        }
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < m; j++) {
                int num; num = getint ();
                add_edge (get (i, j), get (i+1, j)+n*m, 1, num);
                add_edge (get (i+1, j), get (i, j)+n*m, 1, num);
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m-1; j++) {
                int num; num = getint ();
                add_edge (get (i, j), get (i, j+1)+n*m, 1, num);
                add_edge (get (i, j+1), get (i, j)+n*m, 1, num);
            }
        }
        long long cost;
        int res;
        res = MCMF (s, t, cost);
        if (res != maxflow) {
            puts ("-1");
        }
        else 
            printf ("%lld\n", cost);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值