Poj 3422 Kaka's matrix travels (billing flow)

Source: Internet
Author: User

KaKa's matrix travels
Time limit:1000 ms   Memory limit:65536 K
Total submissions:7743   Accepted:3111

Description

OnN×NChessboard with a non-negative number in each grid, Kaka starts his matrix travelsSum= 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 numberSumIn each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSumKaKa can obtain for his first travel. Now Kaka is wondering what is the maximumSumHe can obtain after hisKTh travel. NoteSumIs accumulative duringKTravels.

Input

The first line contains two integersNAndK(1 ≤N≤ 50, 0 ≤K≤ 10) described above. The followingNLines represents the Matrix. You can assume the numbers in the matrix are no more than 1000.

Output

The maximumSumKaKa can obtain after hisKTh travel.

Sample Input

 
3 21 2 30 2 11 4 2

Sample output

 
15

I can't speak out. Open the array bigger !!

This question needs to be split when creating a graph. Each grid is split into two vertices, one being the output vertex u, and the other being the entry vertex V, and the other being the U-> v.

One capacity is 1, the weight is the amount of money in the grid; the other capacity is K-1, the weight is 0. Because once the money is collected, there is no money.

There is a path from V to the next grid (right and bottom). The capacity is K and the fee is zero.

The super source is 0, and the super sink is N * n * 2 + 1.

# Include "stdio. H "# include" string. H "# include" queue "using namespace STD; # define n 50000 const int INF = 1 <20; struct node {int U, V, C, F, next ;} E [N * 2]; int pre [N], DIS [N], vis [N], head [N], T; void Add1 (int u, int V, int C, int f) {e [T]. U = u; E [T]. V = V; E [T]. C = C; E [T]. F = f; E [T]. next = head [u]; head [u] = T ++;} void add (int u, int V, int C, int f) {Add1 (u, v, c, F); Add1 (v, U,-C, 0);} int spfa (int s, int N) {int I, U, V; for (I = s; I <= N; I + +) Dis [I] = inf; DIS [s] = 0; memset (PRE,-1, sizeof (pre); memset (VIS, 0, sizeof (VIS); queue <int> q; q. push (s); While (! Q. Empty () {u = Q. Front (); q. Pop (); vis [u] = 0; for (I = head [u]; I! =-1; I = E [I]. next) {v = E [I]. v; If (E [I]. F & dis [v]> dis [u] + E [I]. c) {dis [v] = dis [u] + E [I]. c; Pre [v] = I; If (! Vis [v]) {vis [v] = 1; q. Push (v) ;}}} if (pre [N]! =-1) return 1; return 0;} void solve (int s, int N) {int I, j, cost, minf; cost = 0; while (spfa (S, n) {minf = inf; for (I = pre [N]; I! =-1; I = pre [E [I]. u]) {If (minf> E [I]. f) minf = E [I]. f ;}for (I = pre [N]; I! =-1; I = pre [E [I]. u]) {J = I ^ 1; E [I]. f-= minf; E [J]. F + = minf;} cost + = minf * Dis [N];} printf ("% d \ n",-cost);} int main () {int I, j, N, K, U, C; while (scanf ("% d", & N, & K )! =-1) {T = 0; memset (Head,-1, sizeof (head); for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {scanf ("% d", & C); U = (I-1) * n + J; add (u, u + N * n,-C, 1); add (u, u + N * n, 0, k-1); if (I + 1 <= N) add (U + N * n, U + N, 0, k); If (J + 1 <= N) add (U + N * n, U + 1, 0, k) ;}} add (, 0, k); add (2 * n, 2 * n +, K ); solve (* n + 1);} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.