博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3422 Kaka's Matrix Travels[费用流]
阅读量:6369 次
发布时间:2019-06-23

本文共 3249 字,大约阅读时间需要 10 分钟。

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 21 2 30 2 11 4 2

Sample Output

15

Source

, Huang, Jinsong

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

 

 
 

转载地址:http://evema.baihongyu.com/

你可能感兴趣的文章
Asp.Net多线程用法1
查看>>
exFAT是支持Mac和Win的
查看>>
(转)postman中 form-data、x-www-form-urlencoded、raw、binary的区别
查看>>
js Date操作
查看>>
判断用户密码是否在警告期内(学习练习)
查看>>
sp_executesql的执行计划会被重用(转载)
查看>>
禅道项目管理软件插件开发
查看>>
Linux系统各发行版镜像下载
查看>>
JS获取键盘按下的键值event.keyCode,event.charCode,event.which的兼容性
查看>>
查看ORACLE 数据库及表信息
查看>>
腾讯、百度、阿里面试经验—(1) 腾讯面经
查看>>
Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
查看>>
HTML DOM 教程Part1
查看>>
GBDT的基本原理
查看>>
MySQL修改root密码的多种方法(转)
查看>>
MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录
查看>>
.NET Core 跨平台发布(dotnet publish)
查看>>
Activity入门(一)
查看>>
CentOS下如何从vi编辑器插入模式退出到命令模式
查看>>
Mysql索引的类型
查看>>