【POJ】 3422 Kaka’s Matrix Travels 费用流

来源:互联网 发布:linux给文件夹重命名 编辑:程序博客网 时间:2024/05/21 22:22
Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7465 Accepted: 3004

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels withSUM = 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 toSUMin each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximumSUM he can obtain after hisKth travel. Note theSUM is accumulative during theK 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 21 2 30 2 11 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], a[maxN];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;    a[s] = 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;                a[v] = min(edge[i].c, a[u]);                if(!inq[v]){                    inq[v] = 1;                    Q[tail++] = v;                }            }        }    }    if(d[t] == oo) return 0;    flow += a[t];    cost += a[t] * d[t];    for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){        edge[i].c -= a[t];        edge[i ^ 1].c += a[t];    }    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;}



 

 

0 0
原创粉丝点击