Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5298 | Accepted: 2059 |
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
Source
简单的费用流,走K次,则用超级源到(1,1)的流量限制为K。
记得要把队列开大一点,我一开始就糗了
#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#define GETIND(a,b) (((a)-1)*n+(b)-1)
long n;long K;
long S;long T;
long ans = 0;
const long inf = 0x7f7f7f7f;
const long maxn = 6010;
long dist[maxn];
long que[1000000];
bool vis[maxn];
struct node
{
long u;
long ind;
long val;
long flow;
node* next;
node* back;
};
node* head[maxn];
node* pre[maxn];
#ifdef Debug
long abcdeg;
#endif
bool spfa()
{
memset(dist,0x80,sizeof dist);
memset(pre,0,sizeof pre);
long l = 0;
long r = 0;
dist[S] = 0;
que[++r] = S;
while (l < r)
{
long u = que[++l];
vis[u] = false;
for (node* vv=head[u];vv;vv=vv->next)
{
long v=vv->ind;
if (vv->flow>0 && dist[v]<dist[u]+vv->val)
{
pre[v] = vv;
dist[v] = dist[u] + vv->val;
if (!vis[v])
{
vis[v] = true;
que[++r] = v;
}
}
}
}
return pre[T];
}
inline long MIN(long a,long b)
{
return a<b?a:b;
}
long MOVE(long a,long d)
{
if (d == 0)
{
if (a > (n-1)*n-1) return -1;
else return a+n;
}
else
{
if ((a%n) == n-1) return -1;
else return a+1;
}
}
void insert(long u,long v,long f,long val)
{
node* nn = new node;
nn-> ind = v;
nn-> u = u;
nn-> next = head[u];
nn-> flow = f;
nn-> val = val;
head[u] = nn;
nn = new node;
nn->ind = u;
nn->u = v;
nn->next = head[v];
nn->flow = 0;
nn->val = -val;
head[v] = nn;
head[u]->back = head[v];
head[v]->back = head[u];
}
void work()
{
while (1)
{
if (!spfa()) break;
node* p = pre[T];long mincut=inf;
while (p)
{
mincut = MIN(mincut,p->flow);
p = pre[p->u];
}
ans += dist[T] * mincut;
p = pre[T];
while (p)
{
p->flow -= mincut;
p->back->flow += mincut;
p = pre[p->u];
}
}
}
inline long getint()
{
long rs=0;char tmp;bool sgn=1;
do tmp = getchar();
while (!isdigit(tmp) && tmp!='-');
if (tmp == '-'){sgn = 0; tmp =getchar();}
do rs = (rs<<3)+(rs<<1)+tmp-'0';
while (isdigit(tmp=getchar()));
return sgn?rs:-rs;
}
int main()
{
freopen("kkmt.in","r",stdin);
freopen("kkmt.out","w",stdout);
n = getint();
K = getint();
S = (GETIND(n,n)<<1)+2;
T = (GETIND(n,n)<<1)+3;
for (long i=1;i<n+1;i++)
for (long j=1;j<n+1;j++)
{
long tmp = getint();
insert(GETIND(i,j)<<1,(GETIND(i,j)<<1)+1,1,tmp);
}
for (long i=1;i<n+1;i++)
{
for (long j=1;j<n+1;j++)
{
long ind = GETIND(i,j);
for (long d=0;d<2;d++)
{
long ind2 = MOVE(ind,d);
if (ind2 > -1)
{
insert(ind<<1,ind2<<1,inf,0);
insert((ind<<1)+1,ind2<<1,inf,0);
}
}
}
}
insert(S,GETIND(1,1)<<1,K,0);
insert(GETIND(n,n)<<1,T,inf,0);
insert((GETIND(n,n)<<1)+1,T,inf,0);
work();
printf("%ld",ans);
return 0;
}