zoukankan      html  css  js  c++  java
  • 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
    ", -MCMF());
    }
    int main(){
        while(~scanf("%d%d", &n, &m)) work();
        return 0;
    }
    POJ 3422
  • 相关阅读:
    1012 The Best Rank (25 分)(排序)
    1011. World Cup Betting (20)(查找元素)
    1009 Product of Polynomials (25 分)(模拟)
    1008 Elevator (20 分)(数学问题)
    1006 Sign In and Sign Out (25 分)(查找元素)
    1005 Spell It Right (20 分)(字符串处理)
    Kafka Connect 出现ERROR Failed to flush WorkerSourceTask{id=local-file-source-0}, timed out while wait
    flume、kafka、avro组成的消息系统
    Java23种设计模式总结【转载】
    Java编程 思维导图
  • 原文地址:https://www.cnblogs.com/ac-luna/p/3759966.html
Copyright © 2011-2022 走看看