POJ训练计划3422_Kaka's Matrix Travels(网络流/费用流)

本文介绍了一种解决Kaka的矩阵旅行问题的方法,通过构建特殊图模型并运用最大流最小费用算法来寻找K次旅行中所能获得的最大累积值。

解题报告

题目传送门

题意:

从n×n的矩阵的左上角走到右下角,每次仅仅能向右和向下走,走到一个格子上加上格子的数,能够走k次。问最大的和是多少。

思路:

建图:每一个格子掰成两个点,分别叫“出点”,“入点”,
入点到出点间连一个容量1。费用为格子数的边。以及一个容量∞,费用0的边。
同一时候。一个格子的“出点”向它右、下的格子的“入点”连边。容量∞,费用0。
源点向(0,0)的入点连一个容量K的边。(N-1,N-1)的出点向汇点连一个容量inf的边。

  
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
struct node {
    int v,cost,cap,next;
} edge[101000];
int head[10000],dis[10000],pre[10000],vis[10000],f[10000],mmap[100][100],cnt,s,t,n,m,k,flow,cost;
void add(int u,int v,int cost,int cap) {
    edge[cnt].v=v;
    edge[cnt].cost=cost;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].cost=-cost;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int _spfa() {
    for(int i=s; i<=t; i++) {
        dis[i]=-1;
        pre[i]=f[i]=vis[i]=0;
    }
    dis[s]=0;
    f[s]=inf;
    pre[s]=-1;
    vis[s]=1;
    queue<int>Q;
    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&&dis[v]<dis[u]+edge[i].cost) {
                dis[v]=dis[u]+edge[i].cost;
                f[v]=min(f[u],edge[i].cap);
                pre[v]=i;
                if(!vis[v]) {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(dis[t]==-1)return 0;
    cost+=dis[t];
    flow+=f[t];
    for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) {
        edge[i].cap-=f[t];
        edge[i^1].cap+=f[t];
    }
    return 1;
}
void mcmf() {
    cost=flow=0;
    while(_spfa());
    printf("%d\n",cost);
}
int dx[]= {0,1};
int dy[]= {1,0};
int main() {
    int i,j;
    scanf("%d%d",&n,&k);
    memset(head,-1,sizeof(head));
    cnt=0;
    m=n*n;
    s=0;
    t=2*m+1;
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++) {
            scanf("%d",&mmap[i][j]);
        }
    }
    add(s,1,0,k);
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++) {
            add(i*n+j+1,m+i*n+j+1,mmap[i][j],1);
            add(i*n+j+1,m+i*n+j+1,0,inf);
            for(int l=0; l<2; l++) {
                int x=i+dx[l];
                int y=j+dy[l];
                if(x>=0&&x<n&&y>=0&&y<n) {
                    add(m+i*n+j+1,x*n+y+1,0,inf);
                }
            }
        }
    }
    add(2*m,t,0,inf);
    mcmf();
    return 0;
}


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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值