POJ--Kaka's Matrix Travels【最大费用最大流 && 经典建图 && 好题】

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

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

题意:

给你一个N*N的矩阵,每个位置都有一定的点权,当你走到一个位置时,你可以获取该位置的点权。现在一个人要从左上角到右下角走K次,每次只能选择向下走或者向右走,问你走K次所能获得的最大权值和。 要求——每个点可以无限走,但点权只能获取一次。


解析:

一个点可以走多次,点权只能获取一次,也就是说我们走到这一点不一定要获得这一点的点权,不同于HDU3376,这点要注意。

还是把每个点 i 拆成 左点 i 和右点 i。建立超级源点,超级汇点。

建图步骤如下:

(1)每个点的 左点连接右点, 流量为1, 费用为点的权值。

(2)每对可达点之间建边, 如 u -->v ,需要建4条边:左u -> 左v,右u -> 左v,左u -> 右v,右u -> 右v。流量为INF(因为我们可以任意选择经过该边的流量(不超过K的前提下),只要它能使最权值和最大化就行了),费用为0。(大家可以模拟一下)。

(3)源点到左起点建边,流量为k,费用为0, 右终点到汇点建边,流量为k,费用为0。

具体为什么这样建边的,我就不多做解释了,大家可以看小比博客,他写的已经够详细了,还不太清楚的可以模拟画一下图。



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
#define maxn 10000
#define maxm 1000000
using namespace std;
int n, k;
int outset;
int inset;
struct node {
    int u, v, cap, flow, cost, next;
};

node edge[maxm];
int head[maxn], cnt;
int per[maxn];
int dist[maxn], vis[maxn];
int map[55][55];

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

void add(int u, int v, int w, int c){
    node E1 = {u, v, w, 0, c, head[u]};
    edge[cnt] = E1;
    head[u] = cnt++;
    node E2 = {v, u, 0, 0, -c, head[v]};
    edge[cnt] = E2;
    head[v] = cnt++;
}

int change(int x, int y){
    return (x - 1) * n + y;
}

void getmap(){
    int t = n * n;
    outset = 0;
    inset = n * n * 2 + 1;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            scanf("%d", &map[i][j]);
                add(change(i, j), change(i, j) + t, 1, map[i][j]);
            if(i + 1 <= n){
                add(change(i, j), change(i + 1, j), INF, 0);
                add(change(i, j) + t, change(i + 1, j), INF, 0);
                add(change(i, j), change(i + 1, j) + t, INF, 0);
                add(change(i, j) + t, change(i + 1, j) + t, INF, 0);
            }
            if(j + 1 <= n){
                add(change(i, j), change(i, j + 1) , INF, 0);
                add(change(i, j) + t, change(i, j + 1), INF, 0);
                add(change(i, j), change(i, j + 1) + t, INF, 0);
                add(change(i, j) + t, change(i, j + 1) + t, INF, 0);
            }
        }
    }
    add(outset, 1, k, 0);
    add(change(n, n) + t, inset, k, 0);
}

bool SPFA(int st, int ed){
    queue<int>q;
    for(int i = 0; i <= inset; ++i){
        dist[i] = -INF;
        vis[i] = 0;
        per[i] = -1;
    }
    dist[st] = 0;
    vis[st] = 1;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(dist[E.v] < dist[u] + E.cost && E.cap > E.flow){
                dist[E.v] = dist[u] + E.cost;
                per[E.v] = i;
                if(!vis[E.v]){
                    vis[E.v] = 1;
                    q.push(E.v);
                }
            }
        }
    }
    return per[ed] != -1;
}

void MCMF(int st, int ed, int &cost, int &flow){
    flow = 0;
    cost = 0;
    while(SPFA(st, ed)){
        int mins = INF;
        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){
            mins = min(mins, edge[i].cap - edge[i].flow);
        }
        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){
            edge[i].flow += mins;
            edge[i ^ 1].flow -= mins;
            cost += edge[i].cost * mins;
        }
        flow += mins;
    }
}
int main (){
    while(scanf("%d%d", &n, &k) != EOF ){
        init();
        getmap();
        int cost, flow;
        MCMF(outset, inset, cost, flow);
        printf("%d\n", cost);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值