Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7097 | Accepted: 2830 |
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define MAX 100
#define MAXN 100001
#define MAXE 1000001
#define IMAX 21474836
struct GROUPS{int f,t,flow,cost,next;};
GROUPS a[MAXE];
int N,K,map[MAX][MAX],tot=0,last[MAXN],dist[MAXN],ans=0,fa[MAXN];
bool vis[MAXN];
void add(int from,int to,int cost,int flow)
{
a[tot].f=from;
a[tot].t=to;
a[tot].next=last[from];
a[tot].flow=flow;
a[tot].cost=cost;
last[from]=tot++;
a[tot].f=to;
a[tot].t=from;
a[tot].next=last[to];
a[tot].flow=0;
a[tot].cost=-cost;
last[to]=tot++;
}
void pre_group()
{
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
int getnum=(i-1)*N+j;
add(getnum,N*N+getnum,-map[i][j],1);
add(getnum,N*N+getnum,0,K-1);
}
for(int i=1;i<N;i++)//down to conect
for(int j=1;j<=N;j++)
{
int nextnum=i*N+j;
int nownum=(i-1)*N+j;
add(N*N+nownum,nextnum,0,K);
}
for(int i=1;i<=N;i++)//right to conect
for(int j=1;j<N;j++)
{
int nextnum=(i-1)*N+j+1;
int nownum=(i-1)*N+j;
add(N*N+nownum,nextnum,0,K);
}
add(0,1,0,K);
add(2*N*N,2*N*N+1,0,K);
}
bool spfa()
{
queue<int> Q;
for(int i=0;i<=2*N*N+1;i++)
dist[i]=IMAX;
memset(fa,-1,sizeof(fa));
memset(vis,false,sizeof(vis));
dist[0]=0;
vis[0]=true;
Q.push(0);
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=last[now];i!=-1;i=a[i].next)
{
int to=a[i].t;
if(dist[now]+a[i].cost<dist[to] && a[i].flow)
{
dist[to]=dist[now]+a[i].cost;
fa[to]=i;
if(!vis[to])
{
vis[to]=true;
Q.push(to);
}
}
}
vis[now]=false;
}
if(dist[2*N*N+1]==IMAX) return false;
return true;
}
void work()
{
while(spfa())
{
int minflow=IMAX;
for(int i=fa[2*N*N+1];i!=-1;i=fa[a[i].f])
minflow=minflow>a[i].flow?a[i].flow:minflow;
for(int i=fa[2*N*N+1];i!=-1;i=fa[a[i].f])
{
a[i].flow-=minflow;
a[i^1].flow+=minflow;
}
ans+=dist[2*N*N+1]*minflow;
}
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
memset(last,-1,sizeof(last));
scanf("%d%d",&N,&K);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
scanf("%d",&map[i][j]);
pre_group();
work();
printf("%d",-ans);
//system("pause");
return 0;
}