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

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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值