【POJ】 3422 Kaka’s Matrix Travels 费用流

这篇博客讨论了一个关于在矩阵中移动并累计最大值的问题。玩家Kaka从矩阵的左上角开始,每次只能向右或向下移动,同时更新通过点的总和。目标是在第K次移动后获得最大的总和。通过构建特殊的图并应用最小费用最大流算法解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7465 Accepted: 3004

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels withSUM = 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 toSUMin each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximumSUM he can obtain after hisKth travel. Note the SUM is accumulative during theK 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

Source

POJ Monthly--2007.10.06, Huang, Jinsong
 
题目大意:K取方格数
 
题目分析:拆点,每个点 i 拆成 i 、i',因为每个格子的价值只能取一次,所以建边(i,i',1,w),又因为每个格子能多次经过,所以建边(i,i',oo,0)。对于其他的点,沿着路径建边即可。
 
代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define REP(i, n) for(int i = 1; i <= n; ++i)
#define MS0(X) memset(X,  0, sizeof X)
#define MS1(X) memset(X, -1, sizeof X)
using namespace std;
const int maxE = 3000000;
const int maxN = 5005;
const int maxM = 55;
const int oo = 0x3f3f3f3f;
struct Edge{
    int v, c, w, n;
};
Edge edge[maxE];
int adj[maxN], l;
int d[maxN], cur[maxN], a[maxN];
int inq[maxN], Q[maxE], head, tail;
int cost, flow, s, t;
int n, m, nn, A[maxM][maxM];
void addedge(int u, int v, int c, int w){
    edge[l].v = v; edge[l].c = c; edge[l].w =  w; edge[l].n = adj[u]; adj[u] = l++;
    edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;
}
int SPFA(){
    memset(d, oo, sizeof d);
    memset(inq, 0, sizeof inq);
    head = tail = 0;
    d[s] = 0;
    a[s] = oo;
    cur[s] = -1;
    Q[tail++] = s;
    while(head != tail){
        int u =  Q[head++];
        inq[u] = 0;
        for(int i = adj[u]; ~i; i = edge[i].n){
            int v = edge[i].v;
            if(edge[i].c && d[v] > d[u] + edge[i].w){
                d[v] = d[u] + edge[i].w;
                cur[v] = i;
                a[v] = min(edge[i].c, a[u]);
                if(!inq[v]){
                    inq[v] = 1;
                    Q[tail++] = v;
                }
            }
        }
    }
    if(d[t] == oo) return 0;
    flow += a[t];
    cost += a[t] * d[t];
    for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){
        edge[i].c -= a[t];
        edge[i ^ 1].c += a[t];
    }
    return 1;
}
int MCMF(){
    flow = cost = 0;
    while(SPFA());
    return cost;
}
void work(){
    REP(i, n) REP(j, n) scanf("%d", &A[i][j]);
    MS1(adj);
    l = 0;
    nn = n * n;
    s = 0;
    t = (nn << 1) + 1;
    addedge(s, 1, m, 0);
    addedge(nn << 1, t, m, 0);
    REP(i, n) REP(j, n){
        int ij = (i - 1) * n + j;
        addedge(ij, ij + nn, 1, -A[i][j]);
        addedge(ij, ij + nn, oo, 0);
        if(i < n) addedge(ij + nn, i * n + j, oo, 0);
        if(j < n) addedge(ij + nn, (i - 1) * n + j + 1, oo, 0);
    }
    printf("%d\n", -MCMF());
}
int main(){
    while(~scanf("%d%d", &n, &m)) work();
    return 0;
}



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值