# POJ 3422 Kaka's Matrix Travels（最大费用最大流 + 拆点）

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

PS:

1、拆点建边：把每个点拆为两个点再在这两个点之间分别建立两条边：

2、再分别向右，向下建一条费用为 0 ，流量为 k 的边！

//最大费用最小流只要在添加边的时候换一下位置就好了

//求最大费用最大流只需要把费用换成相反数，用最小费用最大流求解即可

```#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, next, cap, flow, cost;
int x, y;
} edge[MAXM];
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N, M;
int map[MAXN][MAXN];
void init()
{
N = MAXN;
tol = 0;
}
void addedge(int u, int v, int cap, int cost)//左端点，右端点，容量，花费
{
edge[tol]. to = v;
edge[tol]. cap = cap;
edge[tol]. cost = cost;
edge[tol]. flow = 0;
edge[tol]. to = u;
edge[tol]. cap = 0;
edge[tol]. cost = -cost;
edge[tol]. flow = 0;
}
bool spfa(int s, int t)
{
queue<int>q;
for(int i = 0; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[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)
{
int v = edge[i]. to;
if(edge[i]. cap > edge[i]. flow &&
dis[v] > dis[u] + edge[i]. cost )
{
dis[v] = dis[u] + edge[i]. cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -1) return false;
else return true;
}
//返回的是最大流， cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost)
{
int flow = 0;
cost = 0;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to])
{
if(Min > edge[i]. cap - edge[i]. flow)
Min = edge[i]. cap - edge[i]. 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;
}
return flow;
}

int main()
{
int n, k;
while(~scanf("%d%d",&n,&k))
{
init();//注意
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d",&map[i][j]);
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
int tt = (i-1)*n+j;
//printf("tt:%d tt*2:%d tt*2+1:%d ",tt,tt*2,tt*2+1);
}
//printf("\n");
}
for(int i = 1; i <= n; i++)//向右
{
for(int j = 1; j < n; j++)
{
int tt = (i-1)*n+j;//编号从2开始
//printf("tt:%d tt*2:%d (tt+1)*2:%d\n",tt,tt*2,(tt+1)*2);
}
}
for(int i = 1; i < n; i++)//向下
{
for(int j = 1; j <= n; j++)
{
int tt = (i-1)*n+j;
//printf("tt:%d tt*2:%d (tt+n)*2:%d\n",tt,tt*2,(tt+n)*2);
}
}
int beg = 1;//超级起点
int end = 2*n*n+2;//超级汇点
int ans = 0;
minCostMaxflow(beg,end,ans);
//minCostMaxflow(2,2*n*n+1,ans);
printf("%d\n",-ans);//相反数即为最大的价值
}
return 0;
}
```

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 【最大费用最大流】【好题】

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 mo

## 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,费用为