KaKa's matrix travels
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:2570 |
|
Accepted:964 |
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
Source
Poj monthly -- 2007.10.06, Huang, and Jinsong are split into two shares '! One of the two sides is set to 1, the cost is-data, and the other is infinite capacity, and the cost is 0. Then, one side connects to the right and the following points with unlimited capacity, the cost is 0; # include <iostream>
# Include <queue>
# Include <memory>
Using namespace STD;
# Define INF 0x7ffffff
Const int n= 6000;
Struct fuck
{
Int to, flow, cost;
Int next;
} Node [2, 20000];
Int Dia [60*60];
Int N, TOT, K, en;
Int pre [N];
Int dis [N];
Bool flag [N];
Int CNT [N];
Int road [N];
Int S, T;
Int spfa ()
{
Queue <int> q;
Int now, next;
Pre [0] = 0;
For (INT I = 1; I <= T; I ++)
Dis [I] = inf;
Dis [0] = 0;
Memset (flag, false, sizeof (FLAG ));
Flag [s] = true;
Q. Push (s );
While (! Q. Empty ())
{
Now = Q. Front ();
Q. Pop ();
Flag [now] = false;
Next = CNT [now];
While (next! =-1)
{
If (node [next]. Flow> 0 & dis [node [next]. To]> dis [now] + node [next]. Cost)
{
Dis [node [next]. To] = dis [now] + node [next]. cost;
Pre [node [next]. To] = now;
Road [node [next]. To] = next;
If (! Flag [node [next]. To])
{
Q. Push (node [next]. );
Flag [node [next]. To] = true;
}
}
Next = node [next]. Next;
}
}
If (DIS [T]! = Inf)
Return 1;
Else
Return 0;
}
Int mincostflow ()
{
Int last, Min, ANS = 0;
While (spfa ())
{
Min = inf;
Last = T;
While (last! = S)
{
If (node [road [last]. Flow <min)
Min = node [road [last]. flow;
Last = pre [last];
}
Ans + = min * Dis [T];
Last = T;
While (last! = S)
{
Node [road [last]. Flow-= min;
Node [road [last] ^ 1]. Flow + = min;
Last = pre [last];
}
}
Return ans;
} Void addedge (int from, int to, int cost, int flow)
{
Node [En]. Flow = flow;
Node [En]. Cost = cost;
Node [En]. To =;
Node [En]. Next = CNT [from];
CNT [from] = EN ++;
} Void add (int from, int to, int cost, int flow)
{
Addedge (from, to, cost, flow );
Addedge (to, from,-cost, 0 );
}
Int main ()
{
Int I;
While (scanf ("% d", & N, & K )! = EOF)
{
TOT = N * N;
For (I = 1; I <= tot; I ++)
Scanf ("% d", & Dia [I]);
En = 0;
Memset (CNT,-1, sizeof (CNT ));
S = 0;
T = tot + 1;
For (I = 1; I <= tot; I ++)
{
Add (I, I + tot,-dia [I], 1 );
Add (I, I + tot, 0, INF );
If (I % n! = 0)
Add (I + tot, I + 1, 0, INF );
If (I + n <= ToT)
Add (I + tot, I + N, 0, INF );
}
Add (s, 1, 0, k );
Add (TOT + tot, T, 0, k );
Printf ("% d/N",-mincostflow ());
}
Return 0;
}