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 ...
随机推荐
- 让服务器iis支持.apk文件下载的设置方法
随着智能手机的普及,越来越多的人使用手机上网,很多网站也应手机上网的需要推出了网站客户端,.apk文件就是安卓(Android)的应用程序后缀名,默认情况下,使用IIS作为Web服务器的无法下载此文件 ...
- MyEclipse或者Eclipse内存溢出问题
对于一些项目,我们经常会遇到内存溢出的问题,这个时候我们就需要进行设置JDK的各个内存的大小了. 如果是运行项目以后出现问题的话,那么主要还是jdk或者是tomcat的内存问题,至于网上说的xxx.i ...
- 第 31 章 项目实战-PC 端固定布局[4]
学习要点: 1.热门旅游区 2.标题介绍区 3.旅游项目区 主讲教师:李炎恢 本章主要开始使用学习用 HTML5 和 CSS3 来构建 Web 页面,第一个项目采用 PC 端固定布局来实现. 一.热门 ...
- 03-树1 树的同构 (C语言链表实现)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
- 从零开始学 Java - Spring 一主多从、多主多从 数据库配置
待会苹果要开发布会 我写完这篇文章就准备去看发布会了,因为我买了好几包瓜子和啤酒.由于苹果的保密做的越来越差劲,该曝光的信息差不多全部曝光了,我们这种熬夜看发布会的只不过是让这些信息更加真实,或者说是 ...
- Lind.DDD.Utils.HttpHelper关于对HttpClient的正确使用
回到目录 官方的不一定是对的,机器最能证明一切 不知道从什么时候起,我们在写数据库连接,网络连接,文件操作时会习惯加上using,这种习惯被我们误称为一种模式,但事实上,一切事情都有因有果的,使用us ...
- Eclipse如何发布web项目
目录结构: // contents structure [-] 需要的环境 下载和配置JDK 下载和配置Tomcat 下载Eclipse Eclipse 4.4.0 发布Web步骤 创建server ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q138-Q140)
Question 138 You are designing a SharePoint 2010 application that will deploy a Web Part assembly. ...
- Android 手机卫士--自定义属性
在前面的文章中,已经实现了“设置中心”第一栏的功能以及布局 本文地址:http://www.cnblogs.com/wuyudong/p/5936016.html,转载请注明出处. 自定义属性声明 接 ...
- Android 第一http请求访问慢,以后就快了的问题
android的服务端是用MVC+ef,第一次访问特别慢,第一次以后就快了. 在网上找了很多原因,解决不了.后来发现是应用程序池的问题,准确说是ef的问题,应用程序池被回收了,请求就慢了,