Kaka ' s Matrix travels
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 8729 |
|
Accepted: 3498 |
Description
On the n x n chessboard with a non-negative number in each grid, Kaka starts he matrix travels with SUM = 0. Kaka moves one rook from the left-upper grids to the Right-bottom one, taking care that the rook moves onl Y to the right or down. Kaka adds the number to SUM in each grid of the rook visited, and replaces it with zero. It is a difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering, the maximum SUM He can obtain after his K-th travel. Note the SUM is accumulative during the K travels.
Input
The first line contains the 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 is no more than 1000.
Output
The maximum SUM Kaka can obtain after his K-th travel.
Sample Input
3 21 2 30 2 11 4 2
Sample Output
15
Test instructions: give you a n*n matrix, each position has a certain point right, when you go to a position, you can get the point of the position. Now a person to go from the upper left to the lower right corner of the K-times, each time can only choose to go down or to the right, ask you to go k times can get the maximum weight and. Requirements--Each point can be infinitely gone, but the point right can only be obtained once.
I have written a lecture on this kind of question: Point Me
The map is as follows: Set the super source point sink, the super sink point source.
1,sink to the starting point of the left side, the capacity of K, the cost is 0;
2, split, each point is split into a capacity of 1, the cost for the point right side;
3,<u, v> can reach the relationship between U left->v left, U left->v right, u right->v left, U right->v right. The capacity of the side for the INF, the cost is 0;
4, end to source build edge, capacity is K, cost is 0.
Last run the maximum cost maximum flow, the result is the maximum weight and.
AC Code:
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue > #include <stack> #define MAXN 5000+10#define maxm 1000000+10#define INF 0x3f3f3f3fusing namespace Std;struct edge{int from, to, cap, flow, cost, next;}; Edge Edge[maxm];int HEAD[MAXN], Edgenum;int DIST[MAXN], pre[maxn];bool vis[maxn];int sink, Source;int N, K;int Map[60][60 ];void init () {edgenum = 0; Memset (Head,-1, sizeof (head));} void Addedge (int u, int v, int w, int c) {Edge E1 = {u, V, W, 0, C, Head[u]}; Edge[edgenum] = E1; Head[u]= edgenum++; Edge E2 = {V, u, 0, 0,-C, Head[v]}; Edge[edgenum] = E2; Head[v]= edgenum++;} int point (int x, int y) {return (x-1) * N + y;} void Getmap () {int t = n*n; Sink = 0, Source = 2*t+1; for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j + +) {scanf ("%d", &map[i][j]); Addedge (Points (I, J), Point (I, J) + T, 1, map[i][j]);//Split if (I < N)//Four cases {Addedge (point (I, J) + T, point (I+1, J), INF, 0); Addedge (Point (I, J) + T, point (I+1, J) + T, INF, 0); Addedge (Point (I, J), Point (I+1, J), INF, 0); Addedge (Point (I, J), Point (I+1, J) + T, INF, 0); if (J < N)//Four cases {Addedge (point (I, J) + T, point (I, j+1), INF, 0); Addedge (Point (I, J) + T, point (I, j+1) + t, INF, 0); Addedge (Point (I, J), Point (I, j+1), INF, 0); Addedge (Point (I, J), Point (I, j+1) + t, INF, 0); }}} Addedge (sink, point (1, 1), K, 0); Addedge (Point (n, N) + t, source, K, 0);} BOOL SPFA (int s, int t) {queue<int> Q; memset (Dist,-inf, sizeof (Dist)); Memset (Vis, false, sizeof (VIS)); memset (Pre,-1, sizeof (pre)); Dist[s] = 0; Vis[s] = true; Q.push (s); while (! Q.empty ()) {int u = q.front (); Q.pop (); Vis[u] = false;for (int i = head[u]; i =-1; i = Edge[i].next) {Edge E = Edge[i]; if (Dist[e.to] < Dist[u] + e.cost && e.cap > E.flow) {dist[e.to] = Dist[u] + e.cos T Pre[e.to] = i; if (!vis[e.to]) {vis[e.to] = true; Q.push (e.to); }}}} ' return pre[t]! =-1;} void MCMF (int s, int t, int &cost, int &flow) {cost = flow = 0; while (SPFA (s, t)) {int Min = INF; for (int i = pre[t]; i =-1; i = pre[edge[i^1].to]) {Edge E = Edge[i]; min = min (min, e.cap-e.flow); } for (int i = pre[t]; i =-1; i = pre[edge[i^1].to]) {edge[i].flow + = Min; Edge[i^1].flow = Min; Cost + = Edge[i].cost * Min; } flow + = Min; }}int Main () {while (scanf ("%d%d", &n, &k)! = EOF) {init ();Getmap (); int cost, flow; MCMF (sink, source, cost, flow); printf ("%d\n", cost); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3422 Kaka ' s Matrix travels "maximum cost max Flow" "Good question"