【费用流】Kaka's Matrix Travels

Description
On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels
with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom
one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in
each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum
SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can
obtain after his Kth travel. Note the SUM is accumulative during the K travels.

Input
The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The
following N lines represents the matrix. You can assume the numbers in the matrix are no more
than 1000.

Output
The maximum SUM Kaka can obtain after his Kth travel.

Sample Input
3 2
1 2 3
0 2 1
1 4 2

Sample Output
15

此题考察费用流的应用。把每个点拆成上下两个点,加个超级源,流量设为K,求一次最大费用流即可。
Accode:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#define pos(i, j, x) (((i) * n + (j) << 1) + 1 + (x))

const char fi[] = "number.in";
const char fo[] = "number.out";
const int maxN = 5010, SIZE = 0xffff;
const int INF = 0x3f3f3f3f;

struct Edge
{
    int u, v, f, d; Edge *next, *back;
    Edge(int u, int v, int f, int d, Edge *next):
        u(u), v(v), f(f), d(d), next(next) {}
} *edge[maxN], *pre[maxN]; bool marked[maxN];
int dist[maxN], q[SIZE + 1], N, n, S, T;

void init_file()
{
    freopen(fi, "r", stdin);
    freopen(fo, "w", stdout);
    return;
}

inline int getint()
{
    int res = 0; char tmp;
    while (!isdigit(tmp = getchar()));
    do res = (res << 3) + (res << 1) + tmp - '0';
    while (isdigit(tmp = getchar()));
    return res;
}

inline void Ins(int u, int v, int f, int d)
{
    edge[u] = new Edge(u, v, f, d, edge[u]);
    edge[v] = new Edge(v, u, 0, -d, edge[v]);
    edge[u] -> back = edge[v];
    edge[v] -> back = edge[u];
    return;
}

void readdata()
{
    n = getint(); N = getint();
    T = pos(n - 1, n - 1, 1); S = T + 1;
    for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
    {
        int x = getint();
        if (i < n - 1)
            Ins(pos(i, j, 1), pos(i + 1, j, 0), INF, 0);
        if (j < n - 1)
            Ins(pos(i, j, 1), pos(i, j + 1, 0), INF, 0);
        Ins(pos(i, j, 0), pos(i, j, 1), 1, x);
        Ins(pos(i, j, 0), pos(i, j, 1), INF, 0);
    }
    Ins(S, 1, N, 0);
    return;
}

inline bool Spfa()
{
    memset(dist, ~0x3f, sizeof dist);
    memset(marked, 0, sizeof marked);
    memset(pre, 0, sizeof pre); int f = 0, r = 0;
    dist[q[r++] = S] = 0; marked[S] = 1;
    while (f - r)
    {
        int u = q[f++]; marked[u] = 0; f &= SIZE; //
        for (Edge *p = edge[u]; p; p = p -> next)
        if (p -> f > 0)
        {
            int v = p -> v;
            if (dist[u] + p -> d > dist[v])
            {
                dist[v] = dist[u] + p -> d;
                pre[v] = p;
                if (!marked[v])
                    marked[q[r++] = v] = 1, r &= SIZE; //
            }
        }
    }
    return (bool)pre[T];
}

void work()
{
    int ans = 0;
    while (Spfa())
    {
        int Max_flow = INF;
        for (Edge *p = pre[T]; p; p = pre[p -> u])
            Max_flow = std::min(Max_flow, p -> f);
        for (Edge *p = pre[T]; p; p = pre[p -> u])
        {
            p -> f -= Max_flow;
            p -> back -> f += Max_flow;
        }
        ans += dist[T];
    }
    printf("%d\n", ans);
    return;
}

int main()
{
    init_file();
    readdata();
    work();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值