Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8882 | Accepted: 3557 |
Description
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.
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
题意是给出一个N*N的矩阵,然后从左上角走到右下角,每次走只能是往下或者往右走,走一次就将这个位置上的数字加到sum和中,然后这个位置的数字置为0。从左上到右下走K次,问最大和。
最小费用最大流,把每一个位置拆成两个点,代价为该位置的值,容量为1(代表走一次就将其置为0)。然后前一个点与后一个点再建立一条代价为0,容量为K-1的边,代表你还可以从这里走,但是这个位置的值你是取不到了。再加上一个超级源点与汇点,代价为0,容量为K。跑一次就是结果。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define MAXN 5005
ll res;
int n;
int N, K;
struct EDGE
{
int v;
int reverse;
int cap;
int cost;
int next;
}edge[50005];
int val[MAXN][MAXN];
int que[MAXN], stac[MAXN], head[MAXN], d[MAXN], vis[MAXN], pre[MAXN];
void addedge(int u, int v, int cost, int cap)
{
edge[n].v = v;
edge[n].cost = cost;
edge[n].cap = cap;
edge[n].next = head[u];
edge[n].reverse = n + 1;
head[u] = n++;
edge[n].v = u;
edge[n].cost = -cost;
edge[n].cap = 0;
edge[n].next = head[v];
edge[n].reverse = n - 1;
head[v] = n++;
}
void input()
{
int i, j;
int s, e, ww;
scanf("%d%d", &N, &K);
memset(edge, 0, sizeof(edge));
memset(head, -1, sizeof(head));
n = 0;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
scanf("%d", &val[i][j]);
s = ((i - 1)*N + j) * 2 - 1;
e = ((i - 1)*N + j) * 2;
addedge(s, e, val[i][j], 1);
if (i + 1 <= N)
{
s = ((i - 1)*N + j) * 2;
e = (i*N + j) * 2 - 1;
addedge(s, e, 0, K);
s = ((i - 1)*N + j) * 2 - 1;
e = (i*N + j) * 2 - 1;
addedge(s, e, 0, K-1);
}
if (j + 1 <= N)
{
s = ((i - 1)*N + j) * 2;
e = ((i - 1)*N + j + 1) * 2 - 1;
addedge(s, e, 0, K);
s = ((i - 1)*N + j) * 2 - 1;
e = ((i - 1)*N + j + 1) * 2 - 1;
addedge(s, e, 0, K - 1);
}
}
}
N = N*N * 2;
addedge(0, 1, 0, K);
addedge(N-1, N + 1, 0, K);
addedge(N, N + 1, 0, K);
}
bool spfa()
{
int i, hea = 0, tail = 1;
for (i = 0; i <= N + 1; i++)
{
d[i] = -INF;
vis[i] = 0;
pre[i] = i;
}
d[0] = 0;
que[0] = 0;
vis[0] = 0;
while (hea != tail)
{
int u = que[hea];
for (i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap&&d[v] < d[u] + edge[i].cost)
{
d[v] = d[u] + edge[i].cost;
pre[v] = i;
if (!vis[v])
{
vis[v] = 1;
que[tail++] = v;
if (tail == MAXN)
tail = 0;
}
}
}
vis[u] = 0;
hea++;
if (hea == MAXN)
hea = 0;
}
if (d[N + 1] <= 0)// || d[N + 1] == 0)
{
return false;
}
else
{
return true;
}
}
void compute()
{
int sum = INF;
int u, p;
for (u = N + 1; u != 0; u = edge[edge[p].reverse].v)
{
p = pre[u];
edge[p].cap -= 1;
edge[edge[p].reverse].cap += 1;
res += edge[p].cost;
}
}
void solve()
{
res = 0;
while (spfa())
compute();
printf("%lld\n", res);
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
input();
solve();
//system("pause");
return 0;
}