poj3422

maksyuki 发表于 oj 分类,标签:
0

Kaka's Matrix Travels

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 Ktravels.

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

 

题目类型:最小费用最大流

算法分析:建图时需要拆点,将每个点A拆成点A’和点A”,从A’向A”先连一条容量为1,花费为该点值的有向边,再连一条容量为k-1,花费为0的有向边(第一条边表示第一次到达该位置时可以拿走cost价值的东西,第二条边表示该点在拿完之后没有了,但是可以走k-1步)。若A点存在右边相邻或者是下面相邻的点B,则从A”向B’连一条容量为k,花费为0的有向边。最后再分别建一个源点和汇点,源点和1”相连容量为k,花费为0,2*n*n + 1和汇点相连容量为k,花费为0。最后跑一个最小费用流即可。注意这里边表的个数!!!