描述

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 theK travels.

输入

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.

输出

The maximum SUM Kaka can obtain after his Kth travel.

样例输入

3 2
1 2 3
0 2 1
1 4 2

样例输出

15

来源

POJ Monthly--2007.10.06, Huang, Jinsong

正解:网络流(最小费用最大流)

解题报告:

2016年北京大学信息学奥赛训练营上机考核第三场 B题

  大致题意是给定一个网格图,从左上角走到右下角,并且拿走经过的格子里面的数,只能往右或者往下走。总共走k次,问总和最大是多少。

  因为以前做过一道NOIP的类似题目,所以一见到这道题就马上想DP,结果发现不对。NOIP那道题是双线程DP,空间和时间都是允许的,而这道题是k次,要开2*k维,显然会炸。

  机智的我马上想起以前在codevs上面看过这道题,似乎是一模一样的。并且记得是网络流。然而最后没打出来,并不会建模,还是网络流题目做太少了。

  这道题其实很简单,模型也很容易想到。可以看出本题就是求最大费用最大流,我们把图里的数字改成负数就可以变成最小费用最大流,最后取相反数就可以了。

  我参考了这份博客,讲的还比较清楚:http://blog.csdn.net/qq172108805/article/details/7857503

  

  如上图所示,我们把一个点拆成两个点,并且用两条边相连,一条权值为这个点的权值,流量为1;另一条权值为0,流量为k-1(反向边都要分别添加)

  再加上点之间的相连,容易想到这样建模可以达到题目要求的目的

  代码如下:

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXD = ;
const int MAXM = ;
const int inf = ;
int n,k;
int a[MAXN][MAXN];
int first[MAXD],dis[MAXD];
bool pd[MAXD];
int pre[MAXD];
int ecnt=;
int s,t;
int ans;
queue<int>Q; struct edge{
int to,f,next,u;
int w;
}e[MAXM]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void link(int x,int y,int w,int z){
e[++ecnt].next=first[x]; first[x]=ecnt; e[ecnt].to=y; e[ecnt].f=z; e[ecnt].w=w; e[ecnt].u=x;
e[++ecnt].next=first[y]; first[y]=ecnt; e[ecnt].to=x; e[ecnt].f=; e[ecnt].w=-w; e[ecnt].u=y;
} inline int spfa(){//EK
//根据边权跑最短路
while(!Q.empty()) Q.pop();
memset(pd,,sizeof(pd));
//memset(pre,0,sizeof(pre));
//memset(flow,0,sizeof(flow));
for(int i=;i<=t;i++) dis[i]=inf,pre[i]=-;
pd[s]=; Q.push(s); dis[s]=;
while(!Q.empty()){
int u=Q.front(); Q.pop(); pd[u]=;
//if(u==t) break;
for(int i=first[u];i;i=e[i].next) {
if(e[i].f>) {
int v=e[i].to;
if(dis[v]>dis[u]+e[i].w) {//限制流
dis[v]=dis[u]+e[i].w;
pre[v]=i;
if(!pd[v]) { Q.push(v); pd[v]=; }
}
}
}
}
if(dis[t]==inf) return -;
return ;
} inline int update(){
int total=; int f=inf;
for(int i=pre[t];e[i].u!=s;i=pre[e[i].u]) f=min(f,e[i].f); for(int i=pre[t];e[i].u!=s;i=pre[e[i].u]){
total+=e[i].w*f;
e[i].f-=f;
e[i^].f+=f;
} return total;
} inline void solve(){
while(scanf("%d%d",&n,&k)!=EOF) {
for(int i=;i<=n;i++) for(int j=;j<=n;j++) a[i][j]=getint();
ecnt=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) {
int now=(i-)*n+j-;
link(now*,now*+,-a[i][j],); link(now*,now*+,,k-);//拆成两个点,并且连边
} for(int i=;i<=n;i++) //向右连边
for(int j=;j<n;j++) {//最后一列无需连边
int now=(i-)*n+j-;
link(now*+,now*+,,k);
} for(int i=;i<n;i++) //向下连边
for(int j=;j<=n;j++) {//最后一行无需连边
int now=(i-)*n+j-;
link(now*+,(now+n)*,,k);
} s=n*n*; t=s+;
link(s,,,k); link(s-,t,,k); ans=;
while() {
int daan=spfa();
if(daan==-) break;
ans+=update();
}
printf("%d\n",-ans);
} } int main()
{
solve();
return ;
}

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

  1. poj3422 Kaka's Matrix Travels(最小费用最大流问题)

    /* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...

  2. POJ3422 Kaka's Matrix Travels 【费用流】*

    POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...

  3. POJ3422 Kaka's Matrix Travels[费用流]

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

  4. POJ 3422 Kaka's Matrix Travels

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

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

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

  6. 【poj3422】 Kaka's Matrix Travels

    http://poj.org/problem?id=3422 (题目链接) 题意 N*N的方格,每个格子中有一个数,寻找从(1,1)走到(N,N)的K条路径,使得取到的数的和最大. Solution ...

  7. POJ3422或洛谷2045 Kaka's Matrix Travels

    POJ原题链接 洛谷原题链接 很裸的费用流. 将每个点\(x\)拆成\(x_1,x_2\),并从\(x_1\)向\(x_2\)连一条容量为\(1\),费用为该点的权值的边,以及一条容量为\(+\inf ...

  8. POJ3422:Kaka's Matrix Travels——题解

    http://poj.org/problem?id=3422 题目大意: 从左上角走到右下角,中途取数(数>=0),然后该点的数变为0,求走k的总价值和最大值. ———————————————— ...

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

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

随机推荐

  1. 注册其它地区Apple ID

    不同地区Apple Id需求 iPhone 最近换上了iPhone,想要下载其它国家地区的游戏,需要登录该国的Apple Id才能在itunes里下载. 下面记录一下我注册日本地区的Apple Id过 ...

  2. java11-6 String类的其它功能

    String类的其他功能: 替换功能: String replace(char old,char new) String replace(String old,String new) 去除字符串两空格 ...

  3. Android Activity的生命周期

    一.为什么要了解Activity的生命周期 activity is directly affected by its association withother activities, its tas ...

  4. iOS9新特性——堆叠视图UIStackView

    一.引言 随着autolayout的推广开来,更多的app开始使用自动布局的方式来构建自己的UI系统,autolayout配合storyBoard和一些第三方的框架,对于创建约束来说,已经十分方便,但 ...

  5. iOS宏定义

    1.__OBJC__宏定义作用 在.pch 文件中一般都会自动加上这句宏定义,表示宏内引用的文件确保只被使用Objective-C语言的文件所引用,保证引用关系的清晰.因为在一个OC工程中,可能包含. ...

  6. 层叠上下文(The stacking context)

    MDNThe stacking context 层叠上下文是HTML元素的三维概念,这些HTML元素在一条假想的相对于面向(电脑屏幕的)视窗或者网页的用户的z轴上延伸,HTML元素依据其自身属性按照优 ...

  7. P2P NAT检测和穿越方式

    一.      NAT类型 本文转自:http://www.cnblogs.com/hummersofdie/archive/2013/05/21/3090163.html  1.基本的NAT类型:只 ...

  8. Caffe学习系列(8):solver优化方法

    上文提到,到目前为止,caffe总共提供了六种优化方法: Stochastic Gradient Descent (type: "SGD"), AdaDelta (type: &q ...

  9. RF源码阅读(碎片纪录)-Python积木之contextlib

    参考页面: http://docs.python.org/2/library/contextlib.html contextlib是为了配合with语句来使用的.使用起来更加简洁.本来想写一下,这位同 ...

  10. ubuntu apt-get修改源地址

    亲测搜狐可用,其他备用 1.修改源地址:cp /etc/apt/sources.list /etc/apt/sources.list.bakvim /etc/apt/sources.list 修改之后 ...