| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5806 | Accepted: 2266 |
Description
On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels withSUM = 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 toSUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximumSUM he can obtain after his Kth travel. Note the SUM is accumulative during theK travels.
Input
The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤K ≤ 10) described above. The followingN 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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MIN(a,b) ((a)<(b)?(a):(b))
int mat[55][55];
int n,k;
int head[5005];
int adj[25000];
int next[25000];
int point[25000];
int cost[25000];
int cap[25000];
int pre[5005];
int dis[5005];
bool fl[5005];
int max_flow;
int min_cost;
int edge_cnt;
void add(int u,int v,int cst,int cp)
{
next[edge_cnt]=head[u];
head[u]=edge_cnt;
point[edge_cnt]=v;
adj[edge_cnt]=u;
cost[edge_cnt]=cst;
cap[edge_cnt]=cp;
}
void cost_flow(int src,int tar)
{
while(1)
{
memset(pre,-1,sizeof(pre));
memset(dis,0x3f,sizeof(dis));
memset(fl,0,sizeof(fl));
queue<int>q;
q.push(src);
dis[src]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
fl[u]=0;
for(int e=head[u];e!=-1;e=next[e])
{
if(cap[e]>0&&dis[u]+cost[e]<dis[point[e]])
{
dis[point[e]]=dis[u]+cost[e];
pre[point[e]]=e;
if(!fl[point[e]])
{
fl[point[e]]=1;
q.push(point[e]);
}
}
}
}
if(pre[tar]==-1)break;
int min=INF;
for(int i=tar;pre[i]!=-1;i=adj[pre[i]])
{
min=MIN(min,cap[pre[i]]);
}
for(int i=tar;pre[i]!=-1;i=adj[pre[i]])
{
cap[pre[i]]-=min;
if(pre[i]&1)cap[pre[i]+1]+=min;
else cap[pre[i]-1]+=min;
}
max_flow+=min;
min_cost+=min*dis[tar];
}
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
max_flow=0;
min_cost=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&mat[i][j]);
}
}
edge_cnt=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int p3=(i-1)*n+j;
int p4=(i-1)*n+j+n*n;
edge_cnt++;
add(p3,p4,-mat[i][j],1);
edge_cnt++;
add(p4,p3,mat[i][j],1);
edge_cnt++;
add(p3,p4,0,k);
edge_cnt++;
add(p4,p3,0,0);
if(i<n)
{
int p1=(i-1)*n+j;
int p2=i*n+j;
//edge_cnt++;
//add(p1,p2,0,INF);
//edge_cnt++;
//add(p1,p2+n*n,0,INF);
edge_cnt++;
add(p1+n*n,p2,0,k);
edge_cnt++;
add(p2,p1+n*n,0,0);
//edge_cnt++;
//add(p1+n*n,p2+n*n,0,INF);
}
if(j<n)
{
int p1=(i-1)*n+j;
int p2=(i-1)*n+j+1;
//edge_cnt++;
//add(p1,p2,0,INF);
//edge_cnt++;
//add(p1,p2+n*n,0,INF);
edge_cnt++;
add(p1+n*n,p2,0,k);
edge_cnt++;
add(p2,p1+n*n,0,0);
//edge_cnt++;
//add(p1+n*n,p2+n*n,0,INF);
}
}
}
edge_cnt++;
add(0,1,0,k);
edge_cnt++;
add(1,0,0,0);
edge_cnt++;
add(n*n*2,n*n*2+1,0,k);
edge_cnt++;
add(n*n*2+1,n*n*2,0,0);
cost_flow(0,2*n*n+1);
printf("%d\n",-min_cost);
}
return 0;
}
本文介绍了一个关于在带有非负数的N×N棋盘上进行旅行的问题,目标是在进行K次旅行后获得最大的累积SUM值。每次旅行从左上角到右下角,仅允许向右或向下移动,并收集路径上的数值。
426

被折叠的 条评论
为什么被折叠?



