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.
The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following Nlines represents the matrix. You can assume the numbers in the matrix are no more than 1000.
The maximum SUM Kaka can obtain after his Kth travel.
3 2 1 2 3 0 2 1 1 4 2
15
这题如果k=2,那么可以退化为这题:http://blog.csdn.net/zchahaha/article/details/51503242
稍稍改一下即可做,费用流
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxv = 1e+6;
const int maxe = 1e+7;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge {
int u, v;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
this->cost = cost;
}
};
struct MCFC {
int n, m, s, t;
Edge edges[maxe];
int first[maxv];
int next[maxe];
int inq[maxv];
Type d[maxv];
int p[maxv];
Type a[maxv];
int Q[maxe];
void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
}
void AddEdge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
}
bool BellmanFord(int s, int t, Type &flow, Type &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;
int front, rear;
Q[rear = front = 0] = s;
while (front <= rear) {
int u = Q[front++];
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if (!inq[e.v]) {Q[++rear] = e.v; inq[e.v] = true;}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].u;
}
return true;
}
Type Mincost(int s, int t) {
Type flow = 0, cost = 0;
while (BellmanFord(s, t, flow, cost));
return cost;
}
}H;
int mp[660][660];
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&mp[i][j]);
int s=0,t=2*n*n-1;
H.init(t+1);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
H.AddEdge(i*n+j,i*n+j+n*n,1,-mp[i][j]);
if(i!=0||j!=0) H.AddEdge(i*n+j,i*n+j+n*n,k,0);
if(i!=n-1)
H.AddEdge(i*n+j+n*n,i*n+j+n,k,0);
if(j!=n-1)
H.AddEdge(i*n+j+n*n,i*n+j+1,k,0);
}
H.AddEdge(s,n*n,k-1,0);
H.AddEdge(n*n-1,t,k-1,0);
printf("%d\n",-H.Mincost(s,t));
}
}