您的位置:首页 > 其它

POJ 3422 Kaka's Matrix Travels (最大费用最大流)

2013-05-28 10:00 351 查看
Kaka's Matrix Travels

Time Limit: 1000MS  Memory Limit: 65536K

Total Submissions: 6538  Accepted: 2594

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

做法和最小费用流是一样的,只是在spfa的时候求的是最长路。构图方法为拆点,源点连接左上角的点v,流量为K,费用为0,右下角拆出来的点u'连接汇点,流量为K,费用为0。对于点x和拆出来的x',连接两条边,其中一条流量为1,费用为map[i][j],另外一条流量为K,费用为0。对于相邻的可走的点,比如x,y,则用x'连接y,流量也为K,费用为0.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 5050
#define inf 0xfffffff

using namespace std;

struct node
{
int to,val,cost,next;
}edge[SIZE*50];

int head[SIZE],idx;
int dis[SIZE],pre[SIZE],pos[SIZE],que[SIZE*50],fr,len;
bool vis[SIZE];
int N,K,sc,sk,pt;
int map[64][64];

void addnode(int from,int to,int val,int cost)
{
edge[idx].to = to;
edge[idx].val = val;
edge[idx].cost = cost;
edge[idx].next = head[from];
head[from] = idx++;
edge[idx].to = from;
edge[idx].val = 0;
edge[idx].cost = -cost;
edge[idx].next = head[to];
head[to] = idx++;
}

bool spfa()
{
fr = len = 0;
for(int i=0; i<=pt; i++)
{
dis[i] = -1;
pre[i] = pos[i] = -1;
vis[i] = false;
}
que[len++] = sc;
vis[sc] = true;
dis[sc] = 0;
while(fr < len)
{
int cur = que[fr++];
vis[cur] = false;
for(int i=head[cur]; i!=-1; i=edge[i].next)
{
int to = edge[i].to;
if(edge[i].val > 0 && dis[to] < dis[cur] + edge[i].cost)
{
dis[to] = dis[cur] + edge[i].cost;
pre[to] = cur;
pos[to] = i;
if(!vis[to])
{
vis[to] = true;
que[len++] = to;
}
}
}
}
if(pre[sk] != -1 && dis[sk] > -1)
return true;
return false;
}

int CostFlow()
{
int flow = 0, cost = 0;
while(spfa())
{
int Min = inf;
for(int i=sk; i!=sc; i=pre[i])
Min = min(Min,edge[pos[i]].val);
flow += Min;
cost += Min*dis[sk];
for(int i=sk; i!=sc; i=pre[i])
{
edge[pos[i]].val -= Min;
edge[pos[i]^1].val += Min;
}
}
return cost;
}

int main()
{
while(~scanf("%d%d",&N,&K))
{
idx = 0;
memset(head,-1,sizeof(head));
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
scanf("%d",&map[i][j]);
sc = N*N*2+1, sk = sc + 1, pt = sk + 1;
addnode(sc,1,K,0);
addnode(2*N*N,sk,K,0);
for(int i=1; i<=N; i++)
{
for(int j=1; j<=N; j++)
{
int tem = (i-1)*N+j;
addnode(tem,tem+N*N,1,map[i][j]);
addnode(tem,tem+N*N,K,0);
if(i + 1 <= N)
addnode(tem+N*N,tem+N,K,0);
if(j + 1 <= N)
addnode(tem+N*N,tem+1,K,0);
}
}
int ans = CostFlow();
printf("%d\n",ans);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: