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

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 SUMhe 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

方格取数加强版
每个格子拆成两个点,连一条(1,-a[i][j])的边,在连一条(k-1,0)的边【注意我的t是格子(n,n),k-1是为了防止走了k+1多条边到(n,n)】
格子向右和下连一条(k,0)的边
 
最小费用然后取负(当然直接spfa跑最大也可以)
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=,M=2e4+,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,k,a[N][N],s,t,num;
struct edge{
int v,ne,c,f,w;
}e[M<<];
int cnt,h[N];
inline void ins(int u,int v,int c,int w){
cnt++;
e[cnt].v=v;e[cnt].c=c;e[cnt].f=;e[cnt].w=w;
e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].c=;e[cnt].f=;e[cnt].w=-w;
e[cnt].ne=h[v];h[v]=cnt;
}
inline int id(int i,int j){return (i-)*n+j;}
void build(){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int t=id(i,j);
ins(t,num+t,,-a[i][j]);//printf("%d %d %d %d\n",i,j,t,a[i][j]);
ins(t,num+t,k-,);
if(i!=n) ins(num+t,id(i+,j),k,);
if(j!=n) ins(num+t,id(i,j+),k,);
}
}
int d[N],q[N],head,tail,inq[N],pre[N],pos[N];
inline void lop(int &x){if(x==N)x=;}
bool spfa(){
memset(d,,sizeof(d));
memset(inq,,sizeof(inq));
head=tail=;
d[s]=;inq[s]=;q[tail++]=s;
pre[t]=-;
while(head!=tail){
int u=q[head++];inq[u]=;lop(head);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v]>d[u]+w&&e[i].c>e[i].f){
d[v]=d[u]+w;
pre[v]=u;pos[v]=i;
if(!inq[v])q[tail++]=v,inq[v]=,lop(tail);
}
}
}
return pre[t]!=-;
}
int mcmf(){
int flow=,cost=;
while(spfa()){
int f=INF;
for(int i=t;i!=s;i=pre[i]) f=min(f,e[pos[i]].c-e[pos[i]].f);
flow+=f;cost+=d[t]*f;
for(int i=t;i!=s;i=pre[i]){
e[pos[i]].f+=f;
e[((pos[i]-)^)+].f-=f;
}
}//printf("mcmf %d %d\n",flow,cost);
return cost;
}
int main(int argc, const char * argv[]){
n=read();k=read();num=n*n;
s=;t=num+num;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) a[i][j]=read();
build();
printf("%d",-mcmf());
}
 
 

POJ3422 Kaka's Matrix Travels[费用流]的更多相关文章

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

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

  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(最小费用最大流问题)

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

  4. POJ3422 Kaka's Matrix Travels

    描述 On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels wi ...

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

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

  6. POJ 3422 Kaka's Matrix Travels

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

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

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

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

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

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

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

随机推荐

  1. 流行ORM产品优缺点分析--EntityFramework、NHibernate、PetaPoco

    什么是ORM? ORM的全称是Object Relational Mapping,即对象关系映射.它的实现思想就是将关系数据库中表的数据映射成为对象,以对象的形式展现,这样开发人员就可以把对数据库的操 ...

  2. C#模拟Http请求时出现 基础连接已经关闭 未能为 SSLTLS 安全通道建立信任关系

    //解决方法: //引入命名空间: using System.Security.Cryptography.X509Certificates; using System.Net.Security; // ...

  3. visual studio 2015 开发android

    转载请注明: http://www.cnblogs.com/sunyl/p/5493249.html http://www.cnblogs.com/sunyl/ 最近有不少新闻, 甲骨文向谷歌索赔93 ...

  4. php调用COM组件

    PHP 开启COM组件 1.先到PHP.INI中打开COM选项,com.allow_dcom = true 2.我这里的环境是PHP5.4.7,PHP 5.4.5后,com/dotnet 模块已经成了 ...

  5. eclipse里打开SWT项目找不到source/design的图形UI设计界面

    因为前天重新装了个新版的eclipse, 结果今天打开一个SWT的项目,突然找不到source/design的图形UI设计的两个切换按钮 我把SWT组件重新装了还是找不到.结果后来发现是因为重装ecl ...

  6. MySQL中索引和优化的用法总结

    1.什么是数据库中的索引?索引有什么作用? 引入索引的目的是为了加快查询速度.如果数据量很大,大的查询要从硬盘加载数据到内存当中. 2.InnoDB中的索引原理是怎么样的? InnoDB是Mysql的 ...

  7. Lind.DDD.API核心技术分享

    回到目录 关于Lind.DDD框架里API框架的技术点说明 讲解:张占岭 花名:仓储大叔 主要框架:Lind.DDD 目录 关于Lind.DDD.Authorization 关于授权的原理 关于Api ...

  8. 配置文件(App.config文件)

    1. 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是 co ...

  9. 新建 ASP.NET Core Web API 项目 -- RESTFul 风格 Hello World!

    一.创建一个空项目 请查看 新建 .NET Core 项目 -- Hello World! 一节,新建一个项目:    二.添加引用并修改配置为 Web API (.NET Core 已将 MVC/W ...

  10. [TypeScript] 建立与使用AMD Library

    [TypeScript] 建立与使用AMD Library 前言 使用Visual Studio开发TypeScript项目时,开发人员可以将可重用的程序代码,封装为AMD Library来提供其他开 ...