POJ 3422 Kaka's Matrix Travels 费用流


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

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
题目大意:K取方格数
题目分析:拆点,每个点 i 拆成 i 、i',因为每个格子的价值只能取一次,所以建边(i,i',1,w),又因为每个格子能多次经过,所以建边(i,i',oo,0)。对于其他的点,沿着路径建边即可。
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define REP(i, n) for(int i = 1; i <= n; ++i)
#define MS0(X) memset(X,  0, sizeof X)
#define MS1(X) memset(X, -1, sizeof X)
using namespace std;
const int maxE = 3000000;
const int maxN = 5005;
const int maxM = 55;
const int oo = 0x3f3f3f3f;
struct Edge{
    int v, c, w, n;
};
Edge edge[maxE];
int adj[maxN], l;
int d[maxN], cur[maxN], Minflow;
int inq[maxN], Q[maxE], head, tail;
int cost, flow, s, t;
int n, m, nn, a[maxM][maxM];
void addedge(int u, int v, int c, int w){
    edge[l].v = v; edge[l].c = c; edge[l].w =  w; edge[l].n = adj[u]; adj[u] = l++;
    edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;
}
int SPFA(){
    memset(d, oo, sizeof d);
    memset(inq, 0, sizeof inq);
    head = tail = 0;
    d[s] = 0;
    Minflow = oo;
    cur[s] = -1;
    Q[tail++] = s;
    while(head != tail){
        int u =  Q[head++];
        inq[u] = 0;
        for(int i = adj[u]; ~i; i = edge[i].n){
            int v = edge[i].v;
            if(edge[i].c && d[v] > d[u] + edge[i].w){
                d[v] = d[u] + edge[i].w;
                cur[v] = i;
                Minflow = min(edge[i].c, Minflow);
                if(!inq[v]){
                    inq[v] = 1;
                    Q[tail++] = v;
                }
            }
        }
    }
    if(d[t] == oo) return 0;
    flow += Minflow;
    cost += Minflow * d[t];
    for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){
        edge[i].c -= Minflow;
        edge[i ^ 1].c += Minflow;
    }
    return 1;
}
int MCMF(){
    flow = cost = 0;
    while(SPFA());
    return cost;
}
void work(){
    REP(i, n) REP(j, n) scanf("%d", &a[i][j]);
    MS1(adj);
    l = 0;
    nn = n * n;
    s = 0;
    t = (nn << 1) + 1;
    addedge(s, 1, m, 0);
    addedge(nn << 1, t, m, 0);
    REP(i, n) REP(j, n){
        int ij = (i - 1) * n + j;
        addedge(ij, ij + nn, 1, -a[i][j]);
        addedge(ij, ij + nn, oo, 0);
        if(i < n) addedge(ij + nn, i * n + j, oo, 0);
        if(j < n) addedge(ij + nn, (i - 1) * n + j + 1, oo, 0);
    }
    printf("%d\n", -MCMF());
}
int main(){
    while(~scanf("%d%d", &n, &m)) work();
    return 0;
}
POJ 3422

优质内容筛选与推荐>>
1、微软邀请黑客访问总部 决定每年召开黑客大会
2、php伪静态--隐藏地址实际路径方法
3、编程3总结
4、面向对象-面向对象和面向过程的区别
5、显示的接口实现


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn