poj 3422 Kaka's Matrix Travels 【最大费用最大流】【好题】

Kaka‘s Matrix Travels

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8729   Accepted: 3498

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次所能获得的最大权值和。 要求——每个点可以无限走,但点权只能获取一次。

我写了一个关于这类问题的讲解:点我

建图如下:设置超级源点sink,超级汇点source。

1,sink向起点左点建边,容量为K,费用为0;

2,拆点,每个点拆为容量为1,费用为点权的边;

3,<u, v>可达关系建边u左->v左、u左->v右、u右->v左、u右->v右。边的的容量为INF,费用为0;

4,终点到source建边,容量为K,费用为0。

最后跑一次最大费用最大流,结果就是最大权值和。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#define MAXN 5000+10
#define MAXM 1000000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN], pre[MAXN];
bool vis[MAXN];
int sink, source;
int N, K;
int Map[60][60];
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w, int c)
{
    Edge E1 = {u, v, w, 0, c, head[u]};
    edge[edgenum] = E1;
    head[u]= edgenum++;
    Edge E2 = {v, u, 0, 0, -c, head[v]};
    edge[edgenum] = E2;
    head[v]= edgenum++;
}
int point(int x, int y)
{
    return (x-1) * N + y;
}
void getMap()
{
    int t = N*N;
    sink = 0, source = 2*t+1;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            scanf("%d", &Map[i][j]);
            addEdge(point(i, j), point(i, j) + t, 1, Map[i][j]);//拆点
            if(i < N)//四种情况
            {
                addEdge(point(i, j) + t, point(i+1, j), INF, 0);
                addEdge(point(i, j) + t, point(i+1, j) + t, INF, 0);
                addEdge(point(i, j), point(i+1, j), INF, 0);
                addEdge(point(i, j), point(i+1, j) + t, INF, 0);
            }
            if(j < N)//四种情况
            {
                addEdge(point(i, j) + t, point(i, j+1), INF, 0);
                addEdge(point(i, j) + t, point(i, j+1) + t, INF, 0);
                addEdge(point(i, j), point(i, j+1), INF, 0);
                addEdge(point(i, j), point(i, j+1) + t, INF, 0);
            }
        }
    }
    addEdge(sink, point(1, 1), K, 0);
    addEdge(point(N, N) + t, source, K, 0);
}
bool SPFA(int s, int t)
{
    queue<int> Q;
    memset(dist, -INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(dist[E.to] < dist[u] + E.cost && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;
}
void MCMF(int s, int t, int &cost, int &flow)
{
    cost = flow = 0;
    while(SPFA(s, t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            Edge E = edge[i];
            Min = min(Min, E.cap-E.flow);
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            edge[i].flow += Min;
            edge[i^1].flow -= Min;
            cost += edge[i].cost * Min;
        }
        flow += Min;
    }
}
int main()
{
    while(scanf("%d%d", &N, &K) != EOF)
    {
        init();
        getMap();
        int cost, flow;
        MCMF(sink, source, cost, flow);
        printf("%d\n", cost);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 3422 Kaka's Matrix Travels 【最大费用最大流】【好题】

时间: 2024-08-27 07:04:54

poj 3422 Kaka's Matrix Travels 【最大费用最大流】【好题】的相关文章

POJ 3422 Kaka&#39;s Matrix Travels(费用流)

POJ 3422 Kaka's Matrix Travels 题目链接 题意:一个矩阵,从左上角往右下角走k趟,每次走过数字就变成0,并且获得这个数字,要求走完之后,所获得数字之和最大 思路:有点类似区间k覆盖的建图方法,把点拆了,每个点有值的只能选一次,其他都是无值的,利用费用流,入点出点之间连一条容量1,有费用的边,和一条容量k - 1,费用0的边,然后其他就每个点和右边和下边2个点连边,然后跑费用流 代码: #include <cstdio> #include <cstring&g

poj 3422 Kaka&#39;s Matrix Travels (费用流)

Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7743   Accepted: 3111 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 mo

poj 3422 Kaka&#39;s Matrix Travels 最小费最大流

输入的时候没有取反,一直ole. 这里也是用到拆点,将一个点拆成p和q,这两个之间连接两条路,一条cap=1和cost=矩阵上的值,另一条为cap=k和cost=0.在将0和2*n *n+1看成源点和汇点. #include<stdio.h> #include<string.h> #include<vector> #include<queue> #include<algorithm> using namespace std; const int

POJ 3422 Kaka&#39;s Matrix Travels 【最小费用最大流】

题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题目. 建图自己没想出来,看了大神的建边,把每个点分解成两个点,一个代表进入一个代表出去,然后每个进入和每个出去连边,容量是1价值是这个点的矩阵的数值.然后因为可以不进去,所以起点要和别的矩阵元素的起点建边,终点也要和别的矩阵矩阵元素的起点建边,最后跑下最小费用最大流. 这题最右下角的矩阵元素需要特殊

POJ 3422 Kaka&#39;s Matrix Travels(最大费用最大流 + 拆点)

题目链接:http://poj.org/problem?id=3422 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

poj 3422 Kaka&#39;s Matrix Travels 费用流

题意: 给一个n*n的矩阵,每次从左上角走到右下角并取走其中的数,求走k次能取到的最大和. 分析: 费用流边的容量有限制的作用,费用有求和的作用,对于每个点只能取一次,容易想到把这个点拆成两个点并连上容量为1,费用为该点数的边.但明显有的流要"跳过"这个点,如何处理呢?可以加一条容量为无穷,费用为0的边,这样不参加这点费用计算的流就可以"跳过"这个点了. 代码: //poj 3422 //sep9 #include <iostream> #include

POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem?id=3422 题意:有一个N*N的方格,每个方格里面有一个数字.现在卡卡要从左上角走到右下角,规定每次只能向下或者向右走,每次走到一个格子,将得到该格子的数字,并且该格子的数字变为0.当卡卡走一次时,很容易求出最大值,问卡卡走k次,能够得到的最大值为多少. 思路:最小费用最大流 关键是如何构图 1. 将N*N个格点拆分为两个点(i,i + N*N),每个点之间连一条流量为1,费用为

POJ 3422 Kaka&#39;s Matrix Travels(网络流之费用流)

题目地址:POJ 3422 方法是对每个点i拆点成i'和i'',然后对每个i'和i''连一条费用为该点值,流量为1的边,再连1条费用为0,流量为k-1的边. 然后对每个点与右边下边相邻的点连边,流量均为INF,费用均为0.需要再建一源点与汇点,对于k次只需要在源点与汇点处进行限制即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #inclu

POJ 3422 Kaka&#39;s Matrix Travels

K路最大费用最大流, 每个点的值只能取一次: 拆点,一个点的两个部分之间连 1 条费用mp容量一的边,连一条费用0容量很大的边 K次: 源点和汇点拆点,两个部分之间连K条边 Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7985   Accepted: 3191 Description On an N × N chessboard with a non-negative num