POJ 3422 Kaka's Matrix Travels
来源:互联网 发布:数据库可视化工具 编辑:程序博客网 时间:2024/05/18 22:12
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 SUM he 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
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#define N 1000000#define M 10000using namespace std;int n,k,sta,end;struct num{ int sta,end,fan,next,cap,w;}a[N];int b[M],Top,pt[M],dis[M];bool in[M];int main(){ //freopen("data.in","r",stdin); void addeage(int x,int y,int cap,int w,int fan); int EK(); while(scanf("%d %d",&n,&k)!=EOF) { sta = 0; end = n*n*2+1; Top = 0; memset(b,-1,sizeof(b)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int w; scanf("%d",&w); int x = (i-1)*n+j; int y = n*n+((i-1)*n+j); addeage(x,y,1,w,Top+1); addeage(y,x,0,-w,Top-1); addeage(x,y,k-1,0,Top+1); addeage(y,x,0,0,Top-1); } } for(int i=1;i<=n-1;i++) { for(int j=1;j<=n-1;j++) { int x = (i-1)*n+j+n*n; int y = (i-1)*n+j+1; addeage(x,y,k,0,Top+1); addeage(y,x,0,0,Top-1); y = i*n+j; addeage(x,y,k,0,Top+1); addeage(y,x,0,0,Top-1); } } for(int i=1;i<=n-1;i++) { int x = (i-1)*n+n+n*n; int y = i*n+n; addeage(x,y,k,0,Top+1); addeage(y,x,0,0,Top-1); } for(int j=1;j<=n-1;j++) { int x = (n-1)*n+j+n*n; int y = (n-1)*n+j+1; addeage(x,y,k,0,Top+1); addeage(y,x,0,0,Top-1); } addeage(0,1,k,0,Top+1); addeage(1,0,0,0,Top-1); addeage(2*n*n,end,k,0,Top+1); addeage(end,2*n*n,0,0,Top-1); int ans = EK(); printf("%d\n",ans); } return 0;}void addeage(int x,int y,int cap,int w,int fan){ a[Top].sta = x; a[Top].end = y; a[Top].cap = cap; a[Top].w = w; a[Top].fan = fan; a[Top].next = b[x]; b[x] = Top++;}int spfa(){ pt[sta] = 0; queue<int>que; memset(dis,-1,sizeof(dis)); memset(in,false,sizeof(in)); que.push(sta); in[sta] = true; dis[sta] = 0; while(!que.empty()) { int x= que.front(); in[x] = false; que.pop(); for(int i = b[x];i!=-1;i=a[i].next) { int y = a[i].end; int val = a[i].w; if(a[i].cap>0&&dis[y]<(dis[x]+val)) { dis[y] = dis[x] + val; pt[y] = i; if(!in[y]) { que.push(y); in[y] = true; } } } } if(dis[end]==-1) { return 0; }else { return dis[end]; }}int EK(){ int res = 0; while(true) { int ans = spfa(); if(ans==0) { break; } res +=ans; int x = end; while(x!=sta) { int pos = pt[x]; a[pos].cap-=1; int fan = a[pos].fan; a[fan].cap+=1; x = a[pos].sta; } } return res;}
- poj 3422 Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels
- POJ-3422-Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels
- POJ 3422:Kaka's Matrix Travels
- poj 3422 Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels
- poj Kaka's Matrix Travels
- poj 3422 Kaka's Matrix Travels 最小费用最大流
- Kaka's Matrix Travels POJ 3422 最大费用流
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels 费用流
- POJ 3422 Kaka's Matrix Travels 最小费用流
- poj-3422-Kaka's Matrix Travels-最小费用最大流
- OCP-1Z0-053-200题-63题-640
- 字符串比较——最简单的方法(有点水)
- SQL Nexus Tool
- 堆和栈的区别——给初学者
- 正则表达式高级用法\1
- POJ 3422 Kaka's Matrix Travels
- 块设备驱动程序之一
- SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结
- freemarker 简单模板
- Hadoop常用命令
- xcode安装失败
- 腾讯敏捷开发及快速迭代
- eclipse for java EE中jdbc配置与连接测试
- tomcat源码剖析之总体印象[tomcat工作原理]