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

Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8050 Accepted: 3224

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

POJ Monthly--2007.10.06, Huang, Jinsong

题目大意:
有一个N*N的矩阵 从左上角出发 ,可以向下或者向右走,然后走到终点。
每个点上有一个数字,走过之后可以取得上面的数字,重复走只能取一次。
问:如果从左上角走到右下角,最多走K次。可以获得的最大价值是多少?

解题思路:
联想到拆点费用流,此题特点是:每个点被走过一次之后再走的话后面就不能提供费用了。
那么可以增加2条边(一条是1,-a[i][j],另一条是.INF,0)。那么第一次走这个点的时候可以得到 -a[i][j]的费用 但是后面走的时候就不能得到费用了。

代码如下:(比较容易看懂)

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 6000
#define maxm 24000
#define INF 0x3f3f3f3f
using namespace std;

struct Edge
{
    int from,to,next,f,c;
}es[maxm];
int cnt,p[maxn],inq[maxn];
int dis[maxn],ff[maxn],q[maxn],pre[maxn];
int n,m;

int s,t,ans_c;

void add(int x,int y,int f,int c)
{
    es[cnt].from = x;
    es[cnt].to = y;
    es[cnt].f = f;
    es[cnt].c = c;
    es[cnt].next = p[x];
    p[x] = cnt++;

    es[cnt].from = y;
    es[cnt].to = x;
    es[cnt].f = 0;
    es[cnt].c = -c;
    es[cnt].next = p[y];
    p[y] = cnt++;
}

bool bfs()
{
    memset(dis,63,sizeof dis);
    memset(ff,0,sizeof ff);
    memset(inq,0,sizeof inq);
    dis[s] = 0;
    int head = 0,tail = 0;
    q[tail++] = s;
    inq[s] = true;
    ff[s] = INF;
    while(head != tail)
    {
        int u = q[head++];
        if(head == maxn)
            head = 0;
        inq[u] = false;

        for(int i = p[u];~i;i = es[i].next)
        {
            int v = es[i].to;
            if(es[i].f<= 0) continue;
            if(dis[v] > dis[u]+es[i].c)
            {
                dis[v] = dis[u]+ es[i].c;
                pre[v] = i;
                ff[v] = min(ff[u],es[i].f);
                if(!inq[v])
                {
                    q[tail++] = v;
                    inq[v] = true;
                    if(tail == maxn) tail = 0;

                }
            }
        }
    }
    if(dis[t] == INF) return false;
    ans_c += ff[t]*dis[t];
    int u = t;
    while(u != s)
    {
        es[pre[u]].f -= ff[t];
        es[pre[u]^1].f += ff[t];
        u = es[pre[u]].from;
    }
    return true;
}

void Min_Cost()
{
    ans_c = 0;
    while(bfs())
        ;
}

void init()
{
    cnt = 0;
    memset(p,-1,sizeof p);
}

int a[51][51];

int ID(int i,int j)
{
    return (i-1)*n+j;
}

int main()
{
    while(scanf("%d %d",&n,&m) != EOF)
    {
        int tmp = n*n;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        init();
        s = 0,t = 2*tmp+1;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                add(ID(i,j),ID(i,j)+tmp,1,-a[i][j]);
                add(ID(i,j),ID(i,j)+tmp,INF,0);
                if(i != n)
                {
                    add(ID(i,j)+tmp,ID(i+1,j),INF,0);
                }
                if(j != n)
                {
                    add(ID(i,j)+tmp,ID(i,j+1),INF,0);
                }
            }
        }
        add(s,ID(1,1),m,0);
        add(ID(n,n)+tmp,t,m,0);
        /*for(int i = 0;i < cnt;i+=2)
            printf("%d %d\n",es[i].from,es[i].to);*/
        Min_Cost();
        printf("%d\n",-ans_c);
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值