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

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

Source

POJ Monthly--2007.10.06, Huang, Jinsong

[Submit]   [Go Back]   [Status]   [Discuss]

有一个NxN的棋盘,每个格子有一个非负整数。从左上角走到右下角,获得路径格子上的权值(只能向右或向下走),且每个格子的权值只能获得一次,可以理解为经过的格子权值置为0。可以走K次,求获得的最大权值和。

拆点跑最大费用最大流。

每个格子拆成入点和出点,入点向出点连一条容量为1,权值为格子权值的边,表示权值只能获得一次,再连一条容量无穷,权值为0的的边,表示之后可以经过但不获得权值。再按照右下的转移规则连接格子即可。做从左上格子到右下格子,最大流为K的最大费用流即可。

 #include <cstdio>
#include <cstring> #define fread_siz 1024 inline int get_c(void)
{
static char buf[fread_siz];
static char *head = buf + fread_siz;
static char *tail = buf + fread_siz; if (head == tail)
fread(head = buf, , fread_siz, stdin); return *head++;
} inline int get_i(void)
{
register int ret = ;
register int neg = false;
register int bit = get_c(); for (; bit < ; bit = get_c())
if (bit == '-')neg ^= true; for (; bit > ; bit = get_c())
ret = ret * + bit - ; return neg ? -ret : ret;
} inline int min(int a, int b)
{
return a < b ? a : b;
} const int inf = 2e9; const int N = ;
const int M = ; int n, m;
int s, t;
int edges;
int hd[M];
int nt[M];
int to[M];
int fl[M];
int vl[M]; int dis[M];
int pre[M]; inline bool bfs(void)
{
static int que[M];
static int inq[M];
static int head, tail; memset(dis, -, sizeof(dis));
memset(inq, , sizeof(inq));
head = , tail = ;
que[tail++] = s;
pre[s] = -;
dis[s] = ;
inq[s] = ; while (head != tail)
{
int u = que[head++], v; inq[u] = ;
for (int i = hd[u]; ~i; i = nt[i])
if (dis[v = to[i]] < dis[u] + vl[i] && fl[i])
{
pre[v] = i ^ ;
dis[v] = dis[u] + vl[i];
if (!inq[v])inq[que[tail++] = v] = ;
}
} return dis[t] != -;
} inline int minCost(void)
{
int cost = ; while (bfs())
{
int flow = inf; for (int i = pre[t]; ~i; i = pre[to[i]])
flow = min(flow, fl[i ^ ]); for (int i = pre[t]; ~i; i = pre[to[i]])
fl[i] += flow, fl[i ^ ] -= flow; cost += dis[t] * flow;
} return cost;
} inline void add(int u, int v, int f, int w)
{
nt[edges] = hd[u]; to[edges] = v; fl[edges] = f; vl[edges] = +w; hd[u] = edges++;
nt[edges] = hd[v]; to[edges] = u; fl[edges] = ; vl[edges] = -w; hd[v] = edges++;
} int G[N][N]; inline int h(int x, int y, int k)
{
return ((x - ) * n + y) << | k;
} signed main(void)
{
n = get_i();
m = get_i(); for (int i = ; i <= n; ++i)
for (int j = ; j <= n; ++j)
G[i][j] = get_i(); memset(hd, -, sizeof(hd)); s = , t = (n*n + ) << | ; for (int i = ; i <= n; ++i)
for (int j = ; j <= n; ++j)
add(h(i, j, ), h(i, j, ), m, ),
add(h(i, j, ), h(i, j, ), , G[i][j]); for (int i = ; i < n; ++i)
for (int j = ; j <= n; ++j)
add(h(i, j, ), h(i + , j, ), m, ); for (int i = ; i <= n; ++i)
for (int j = ; j < n; ++j)
add(h(i, j, ), h(i, j + , ), m, ); add(s, h(, , ), m, );
add(h(n, n, ), t, m, ); printf("%d\n", minCost());
}

@Author: YouSiki

POJ 3422 Kaka's Matrix Travels的更多相关文章

  1. POJ 3422 Kaka's Matrix Travels(费用流)

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

  2. POJ 3422 Kaka's Matrix Travels(最小费用最大流)

    http://poj.org/problem?id=3422 题意 : 给你一个N*N的方格,每个格子有一个数字,让你从左上角开始走,只能往下往右走,走过的数字变为0,走K次,问最大能是多大,累加的. ...

  3. POJ 3422 Kaka's Matrix Travels 【最小费用最大流】

    题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...

  4. POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)

    题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...

  5. POJ 3422 Kaka's Matrix Travels K取方格数

    题目:给出n*n的方格矩阵,现在从左上方走m次到右下方,问m次能够获得的最大价值和. 分析:最大费用流.拆点进行限制每个格子只取一次,假设点x拆成 x,xx,右边(假设有)y,yy,下方(假设有)z, ...

  6. poj 3422 Kaka's Matrix Travels 费用流

    题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...

  7. POJ 3422 Kaka's Matrix Travels(拆点+最大费用流)题解

    题意:小A从左上角走到右下角,每个格子都有一个价值,经过这个格子就把价值拿走,每次只能往下或往右走,问你走k次最多能拿多少价值的东西. 思路:这里有一个限制条件就是经过之后要把东西拿走,也就是每一格的 ...

  8. [poj] 3422 Kaka's Matrix Travels || 最小费用最大流

    原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...

  9. POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

    POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...

随机推荐

  1. Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】

    2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...

  2. CRM 2013 相关下载 / 2013-10-11

        CRM 2013的安装文件,软件开发工具包(Sdk)以及实施指南,在微软官方网站已经有下载了.     具体地址如下: Name Url 发布日期 语言版本 说明 CRM Server htt ...

  3. 简单的学习心得:网易云课堂Android开发第五章SharedPreferences与文件管理

    一.SharedPreferences (1)SharedPreferences能够用来保存一些属于基本数据类型的数据. (2)保存数据,删除数据都是由SharedPreferences的内部接口Ed ...

  4. Android Studio使用时源码到处报红色警告,运行时又没错

    转载地址:http://www.07net01.com/program/2016/04/1452749.html [摘要:正在AS上开辟时,碰到那个题目,翻开全部的Java源文件,右边一起标赤色,找没 ...

  5. pip安装指定版本的package

    起因 最近到一个项目组,用了一套高大上的运维工具来搭建开发环境. 有vagrant控制VirtualBox启动虚拟机.有ansible来运行playbook初始化环境. 然后遇到了一个坑,项目现有的p ...

  6. seL4之hello-2旅途(完成更新)

    seL4之hello-2旅途 2016/11/19 13:15:38 If you like my blog, please buy me a cup of coffee. 回顾上周 seL4运行环境 ...

  7. Laravel-lumen 配置JWT

    具体步骤参照: [ JWT & Lumen ] 第一步 在项目根目录 执行命令 composer require tymon/jwt-auth第二步 在 bootstrap/app.php 的 ...

  8. python随心笔记

    print "hello,world" #打印hello,world print "1+1" #打印字符串 1+1 单引号和双引号是单行字符串 print [& ...

  9. Linux 下dns的搭建

    1.软件安装: redhat默认使用bind作为安装软件 使用yum安装 yum install -y bind bind-chroot bind-utils bind提供主程序 bind-chroo ...

  10. everything + autohotkey的配合使用

    一,everything是文件搜索神奇,瞬间定位到文件,在众多的文件中找到你需要的文件.(百度下载就好,分32位和64位)   二,autohotkey是热键启动设置,方便的打开常用的应用. 直接使用 ...