POJ 3422 Kaka's Martix Travels 费用流模板

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 Nlines 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


这题如果k=2,那么可以退化为这题:http://blog.csdn.net/zchahaha/article/details/51503242

稍稍改一下即可做,费用流

#include <iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>

using namespace std;

const int maxv = 1e+6;
const int maxe = 1e+7;
typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
    int u, v;
    Type cap, flow, cost;
    Edge() {}
    Edge(int u, int v, Type cap, Type flow, Type cost) {
        this->u = u;
        this->v = v;
        this->cap = cap;
        this->flow = flow;
        this->cost = cost;
    }
};

struct MCFC {
    int n, m, s, t;
    Edge edges[maxe];
    int first[maxv];
    int next[maxe];
    int inq[maxv];
    Type d[maxv];
    int p[maxv];
    Type a[maxv];
    int Q[maxe];

    void init(int n) {
        this->n = n;
        memset(first, -1, sizeof(first));
        m = 0;
    }

    void AddEdge(int u, int v, Type cap, Type cost) {
        edges[m] = Edge(u, v, cap, 0, cost);
        next[m] = first[u];
        first[u] = m++;
        edges[m] = Edge(v, u, 0, 0, -cost);
        next[m] = first[v];
        first[v] = m++;
    }

    bool BellmanFord(int s, int t, Type &flow, Type &cost) {

        for (int i = 0; i < n; i++) d[i] = INF;
        memset(inq, false, sizeof(inq));
        d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;
        int front, rear;
        Q[rear = front = 0] = s;
        while (front <= rear) {
            int u = Q[front++];
            inq[u] = false;
            for (int i = first[u]; i != -1; i = next[i]) {
                Edge& e = edges[i];
                if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
                    d[e.v] = d[u] + e.cost;
                    p[e.v] = i;
                    a[e.v] = min(a[u], e.cap - e.flow);
                    if (!inq[e.v]) {Q[++rear] = e.v; inq[e.v] = true;}
                }
            }
        }
        if (d[t] == INF) return false;
        flow += a[t];
        cost += d[t] * a[t];
        int u = t;
        while (u != s) {
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -= a[t];
            u = edges[p[u]].u;
        }
        return true;
    }

    Type Mincost(int s, int t) {
        Type flow = 0, cost = 0;
        while (BellmanFord(s, t, flow, cost));
        return cost;
    }
}H;

int mp[660][660];

int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&mp[i][j]);
        int s=0,t=2*n*n-1;
        H.init(t+1);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                H.AddEdge(i*n+j,i*n+j+n*n,1,-mp[i][j]);
                if(i!=0||j!=0)  H.AddEdge(i*n+j,i*n+j+n*n,k,0);
                if(i!=n-1)
                    H.AddEdge(i*n+j+n*n,i*n+j+n,k,0);
                if(j!=n-1)
                    H.AddEdge(i*n+j+n*n,i*n+j+1,k,0);
            }
        H.AddEdge(s,n*n,k-1,0);
        H.AddEdge(n*n-1,t,k-1,0);
        printf("%d\n",-H.Mincost(s,t));
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值