poj_3422_Kaka's Matrix Travels

本文介绍了一个关于矩阵旅行的问题,目标是在限定步数内找到最大的累积数值。通过建立图模型并运用SPFA算法与流量网络算法解决该问题。

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

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.

题解

建图:拆点,把一个点拆成两个点,每两个点连一条边。 设置一个源点和汇点,上模版。

代码:

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
#define inf  1 << 30
#define M 5005
int n, m, nm, ans;
int map[51][51],head[M],qu[M],dis[M];
bool v[M];

struct node {
    int x,y,v,w,next;
} e[100005];
void add(int x, int y, int w, int f){
    e[nm].x=x; e[nm].y=y; e[nm].v=w; e[nm].w=f;
    e[nm].next=head[x];
    head[x]=nm;
    nm++;
    e[nm].x=y; e[nm].y=x; e[nm].v=-w; e[nm].w=0;
    e[nm].next=head[y];
    head[y]=nm;
    nm++;
}

bool spfa() {
    for (int i=0;i<=n*n*2+1;i++){
        qu[i] = -1; dis[i] = inf;  
        v[i] = false;  
    }  
    queue<int>q;
    dis[n*n*2]=0; v[n*n*2]=true;
    q.push(n*n*2);
    int i;
    while (!q.empty()){
        int t=q.front();
        q.pop();
        i=head[t]; v[t]=false;
        while (i!=-1){
            if (e[i].w>0&&dis[e[i].y]>dis[t]+e[i].v){
                dis[e[i].y]=dis[t]+e[i].v;
                qu[e[i].y]=i;
                if (!v[e[i].y]){
                    v[e[i].y]=true;
                    q.push(e[i].y);
                }
            }
            i=e[i].next;
        }
    }
    if (qu[n*n*2+1]==-1) return false;
    return true;
}
void getflow() {
    while (spfa()) {
        int max1=inf;
        int p=qu[n*n*2+1];
        while (p!=-1) {
            max1=min(max1,e[p].w);
            p=qu[e[p].x];
        }
        p=qu[n*n*2+1];
        while (p!=-1){
            e[p].w-=max1;
            e[p^1].w+=max1;
            ans+=max1*e[p].v;
            p=qu[e[p].x];
        }
    }
}

int main() {
    while (cin>>n>>m) {
        int i,j;
        nm=0;
        for (i=1;i<=n;i++)
            for (j=1;j<=n;j++)
                cin>>map[i][j];
        memset(head,-1,sizeof(head));
        for (i=1;i<=n;i++)
            for (j=1;j<=n;j++){
                int x=(i-1)*n+j-1;
                add(x*2,x*2+1,-map[i][j],1);
                add(x*2,x*2+1,0,m-1);
            }
        for (i=1;i<=n;i++)
            for (j=1;j<n;j++){
                int x=(i-1)*n+j-1;
                add(x*2+1,(x+1)*2,0,m);
            }
        for (i=1;i<n;i++)  
            for (j=1;j<=n;j++){
                int x=(i-1)*n+j-1;  
                add(x*2+1,(x+n)*2,0,m);  
            }
        add(n*n*2,0,0,m);
        add(n*n*2-1,n*n*2+1,0,m);
        ans=0;
        getflow();
        cout<<-ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值