POJ3422 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
方格取数加强版
- #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[费用流]的更多相关文章
- poj 3422 Kaka's Matrix Travels 费用流
题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...
- POJ3422 Kaka's Matrix Travels 【费用流】*
POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...
- poj3422 Kaka's Matrix Travels(最小费用最大流问题)
/* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...
- POJ3422 Kaka's Matrix Travels
描述 On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels wi ...
- 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
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9567 Accepted: ...
- POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)
题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...
- [poj] 3422 Kaka's Matrix Travels || 最小费用最大流
原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...
- POJ3422或洛谷2045 Kaka's Matrix Travels
POJ原题链接 洛谷原题链接 很裸的费用流. 将每个点\(x\)拆成\(x_1,x_2\),并从\(x_1\)向\(x_2\)连一条容量为\(1\),费用为该点的权值的边,以及一条容量为\(+\inf ...
随机推荐
- .Net中的并行编程-3.ConcurrentQueue实现与分析
在上文<.Net中的并行编程-2.ConcurrentQueue的实现与分析> 中解释了无锁的相关概念,无独有偶BCL提供的ConcurrentQueue也是基于原子操作实现, 由于Con ...
- 基于CkEditor实现.net在线开发之路(4)快速布局,工具箱,模板载入,tab选项卡简单说明与使用
上一章给常用的from表单控件属性页面,进行了简单说明和介绍,但是由于是在网页中做界面设计,操作肯定没有桌面应用程序方便,便捷,为了更方便的布局与设计,今天我主要说一下快速布局,工具箱,tab选项卡, ...
- 最好的Angular2表格控件
现在市面上有大量的JavaScript数据表格控件,包括开源的第三方的和自产自销的.可以说Wijmo的Flexgrid是目前适应Angular 2的最好的表格控件. Angular 2数据表格基本要求 ...
- [教学] Delphi Berlin 10.1 开发 Windows 10 平板 App 远程调试
Delphi Berlin 10.1 开发 Windows 10 平板 App 远程调试安装步骤: 准备电脑: 一台开发电脑,安装 Delphi 开发环境 一台平板电脑,安装 PAServer,安装方 ...
- C++11之lambda表达式
lambda表达式源于函数式编程的概念,它可以就地匿名定义目标函数或函数对象,不需要额外写一个命名函数或者函数对象.lambda表达式的类型在C++11中被称为"闭包类型",也可以 ...
- python 优矿自动化交易
一.进入官网,打开notebook 自己新建 notebook 二.在代码中编写自己的交易策略 https://uqer.io/help/faqApi/#account相关属性 在帮助文档中可以找到 ...
- win7下 VirtualBox虚拟机开机后台自启动
win7下安装个linux虚拟机,学习下非常好. 但是每次使用linux的时候,都是打开virtualBox-->启动安装的linux系统-->再用远程桌面(SSH等)连接 每次手动打开比 ...
- spring面试题(2)
f-sp-1. Spring的aop你怎样实现? 用动态代理和cglib实现,有接口的用动态代理,无接口的用cglib f-sp-2. Spring在SSH起什么作用 整合作用 f-sp-3. Spr ...
- checkbox & radio 的对齐问题
不仅不同浏览器不同,不同的字体,不同的文字大小也会表现不一样. 重置 form checkbox & radio 因为不同浏览器解析不一样,有些是默认margin,有些是默认padding,还 ...
- hibernate(3) —— 关系映射
hibernate中关系映射指的是实体类与实体类间的关系.和数据库中表与表之间的关系类似,有一对一,多对一,一对多,多对多四种映射关系. 一:一对一映射 两个对象之间是一对一的关系,如人和身份证之间是 ...