POJ 3422 Kaka's Matrix Travels
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 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的更多相关文章
- POJ 3422 Kaka's Matrix Travels(费用流)
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6792 Accepted: ...
- POJ 3422 Kaka's Matrix Travels(最小费用最大流)
http://poj.org/problem?id=3422 题意 : 给你一个N*N的方格,每个格子有一个数字,让你从左上角开始走,只能往下往右走,走过的数字变为0,走K次,问最大能是多大,累加的. ...
- POJ 3422 Kaka's Matrix Travels 【最小费用最大流】
题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...
- POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)
题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...
- POJ 3422 Kaka's Matrix Travels K取方格数
题目:给出n*n的方格矩阵,现在从左上方走m次到右下方,问m次能够获得的最大价值和. 分析:最大费用流.拆点进行限制每个格子只取一次,假设点x拆成 x,xx,右边(假设有)y,yy,下方(假设有)z, ...
- poj 3422 Kaka's Matrix Travels 费用流
题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...
- POJ 3422 Kaka's Matrix Travels(拆点+最大费用流)题解
题意:小A从左上角走到右下角,每个格子都有一个价值,经过这个格子就把价值拿走,每次只能往下或往右走,问你走k次最多能拿多少价值的东西. 思路:这里有一个限制条件就是经过之后要把东西拿走,也就是每一格的 ...
- [poj] 3422 Kaka's Matrix Travels || 最小费用最大流
原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...
- POJ 3422 Kaka's Matrix Travels (最小费用最大流)
POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...
随机推荐
- WCF实现事件通知相关应用技巧介绍
WCF实现事件通知是一个比较容易掌握的知识点,不过在实现的过程中,我们还是需要注意一些事项,以保证功能的完善性. WCF中有一些方法的应用对于初学者来说还是比较容易应用.只要熟练的联系这些方法操作,一 ...
- Windows消息机制
Windows的消息系统是由3个部分组成的: · 消息队列.Windows能够为所有的应用程序维护一个消息队列.应用程序必须从消息队列中获取消息,然后分派给某个窗口.· 消息循环.通过这个循环机制应用 ...
- 在 ASP.NET CORE 中使用 SESSION
Session 是保存用户和 Web 应用的会话状态的一种方法,ASP.NET Core 提供了一个用于管理会话状态的中间件.在本文中我将会简单介绍一下 ASP.NET Core 中的 Session ...
- Action.c(58): Error -27796: Failed to connect to server "hostname"
分析: 因为负载生成器的性能太好发数据特别快,服务器响应也特别快,从而导致负载生成器的端口在没有timeout之前就全部占满了. 解决方案一: 在负载生成器的注册表HKEY_LOCAL_MACHI ...
- 读<<领域驱动设计-软件核心复杂性应对之道>>有感
道可道,非常道. 名可名,非常名. 无名天地之始,有名万物之母. ---老子 关于标题 好久没写东西了,动笔的动机是看完了一本书,想写点总结性的东西,一是为了回顾一下梳理知识点,二是为了日后遗忘时能有 ...
- React Native APP结构探索
APP结构探索 我在Github上找到了一个有登陆界面,能从网上获取新闻信息的开源APP,想来研究一下APP的结构. 附上原网址:我的第一个React Native App 具体来讲,就是研究一个复杂 ...
- [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1
Erlang 增加 Maps数据类型并不是很突然,因为这个提议已经进行了2~3年之久,只不过Joe Armstrong老爷子最近一篇文章Big changes to Erlang掀起不小了风 ...
- nginx 安全优化
http://nginx.org/en/docs/http/ngx_http_access_module.html 官网 1.允许特定的ip访问,拒绝特定ip server { listen 80; ...
- [jquery]if条件句
//写个网页用了多门语言,脑袋转不过来亚! //代码: if(){} else if(){} else {}
- Linux多线程编程
线程和进程的区别是: 进程是拷贝主进程的数据段和代码段的. 线程是和主进程公用一份数据段和代码段的. 线程一旦创建,就从指定的入口函数开始执行. 相关头文件: #include <pthread ...