Kaka's Matrix Travels (最大费用流)

Kaka's Matrix Travels

Time Limit:1000MS  Memory Limit:65536K
Total Submit:6 Accepted:3

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 2
1 2 3
0 2 1
1 4 2

 

Sample Output

15

 

Source

POJ Monthly--2007.10.06, Huang, Jinsong

 

 

 

题目大意:给你一个n*n(1<= n <= 50)的矩阵,每一个格子里面有一个值array[i][j]。假设第i行第j列的坐标为(i,j),那么点(i,j)只能走到点(i+1,j)和点(i,j+1),即只能向右和向下走。问从点(1,1)到点(n,n)走K次能够获得的最大值,每一个格子只有在第一次走过的时候总值才会加。

解题思路:最大费用最大流。将每个格子看成一个点A,然后把这个点A拆成两个点A和A'。如果点B在点A的右侧或下侧,那么点A'就与点B连一条费用为0,容量为无穷大的边。对于每两个点A和A',点A向点A'连一条费用为点A这个格子的值、容量为1的边和一条费用为0,容量为无穷大的边。再建立一个源点S。S与点C(1,1)连一条费用为0,容量为K的边,汇点定为点D'(n,n).然后可以求从点S到点D'(n,n)的最大费用最大流。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值