[Billing method] Kaka's matrix travels

Source: Internet
Author: User
DescriptionOn an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travelswith SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottomone, taking care that the rook moves only to the right or down. Kaka adds the number to SUM ineach 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 maximum SUM he canobtain after his Kth travel. Note the SUM is accumulative during the K travels.InputThe first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. Thefollowing N lines represents the matrix. You can assume the numbers in the matrix are no morethan 1000.OutputThe maximum SUM Kaka can obtain after his Kth travel.Sample Input3 21 2 30 2 11 4 2Sample Output15

This topic describes the application of the billing flow. Split each vertex into the upper and lower vertices, add a super source, set the traffic to k, and calculate the maximum charge flow once.
Accode:

#include <cstdio>#include <cstdlib>#include <algorithm>#include <string>#include <cstring>#define pos(i, j, x) (((i) * n + (j) << 1) + 1 + (x))const char fi[] = "number.in";const char fo[] = "number.out";const int maxN = 5010, SIZE = 0xffff;const int INF = 0x3f3f3f3f;struct Edge{    int u, v, f, d; Edge *next, *back;    Edge(int u, int v, int f, int d, Edge *next):        u(u), v(v), f(f), d(d), next(next) {}} *edge[maxN], *pre[maxN]; bool marked[maxN];int dist[maxN], q[SIZE + 1], N, n, S, T;void init_file(){    freopen(fi, "r", stdin);    freopen(fo, "w", stdout);    return;}inline int getint(){    int res = 0; char tmp;    while (!isdigit(tmp = getchar()));    do res = (res << 3) + (res << 1) + tmp - '0';    while (isdigit(tmp = getchar()));    return res;}inline void Ins(int u, int v, int f, int d){    edge[u] = new Edge(u, v, f, d, edge[u]);    edge[v] = new Edge(v, u, 0, -d, edge[v]);    edge[u] -> back = edge[v];    edge[v] -> back = edge[u];    return;}void readdata(){    n = getint(); N = getint();    T = pos(n - 1, n - 1, 1); S = T + 1;    for (int i = 0; i < n; ++i)    for (int j = 0; j < n; ++j)    {        int x = getint();        if (i < n - 1)            Ins(pos(i, j, 1), pos(i + 1, j, 0), INF, 0);        if (j < n - 1)            Ins(pos(i, j, 1), pos(i, j + 1, 0), INF, 0);        Ins(pos(i, j, 0), pos(i, j, 1), 1, x);        Ins(pos(i, j, 0), pos(i, j, 1), INF, 0);    }    Ins(S, 1, N, 0);    return;}inline bool Spfa(){    memset(dist, ~0x3f, sizeof dist);    memset(marked, 0, sizeof marked);    memset(pre, 0, sizeof pre); int f = 0, r = 0;    dist[q[r++] = S] = 0; marked[S] = 1;    while (f - r)    {        int u = q[f++]; marked[u] = 0; f &= SIZE; //        for (Edge *p = edge[u]; p; p = p -> next)        if (p -> f > 0)        {            int v = p -> v;            if (dist[u] + p -> d > dist[v])            {                dist[v] = dist[u] + p -> d;                pre[v] = p;                if (!marked[v])                    marked[q[r++] = v] = 1, r &= SIZE; //            }        }    }    return (bool)pre[T];}void work(){    int ans = 0;    while (Spfa())    {        int Max_flow = INF;        for (Edge *p = pre[T]; p; p = pre[p -> u])            Max_flow = std::min(Max_flow, p -> f);        for (Edge *p = pre[T]; p; p = pre[p -> u])        {            p -> f -= Max_flow;            p -> back -> f += Max_flow;        }        ans += dist[T];    }    printf("%d\n", ans);    return;}int main(){    init_file();    readdata();    work();    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.