【POJ】 3422 Kaka’s Matrix Travels 费用流
来源:互联网 发布:linux给文件夹重命名 编辑:程序博客网 时间:2024/05/21 22:22
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
#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;}
- poj Kaka's Matrix Travels 费用流
- poj 3422 Kaka's Matrix Travels 最小费用最大流
- Kaka's Matrix Travels POJ 3422 最大费用流
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels (最大费用最大流)
- POJ 3422 Kaka's Matrix Travels 费用流
- POJ 3422 Kaka's Matrix Travels 最小费用流
- poj-3422-Kaka's Matrix Travels-最小费用最大流
- POJ 3422 Kaka's Matrix Travels 最小费用最大流
- poj 3422 Kaka's Matrix Travels 费用流
- 【POJ】 3422 Kaka’s Matrix Travels 费用流
- POJ 3422 Kaka's Matrix Travels | 费用流
- poj 3422 Kaka's Matrix Travels (费用流)
- POJ 3422 Kaka's Matrix Travels(费用流)
- 【最大费用最大流】POJ-3422 Kaka's Matrix Travels
- POJ 3422 Kaka's Matrix Travels(费用流)
- POJ 3422 Kaka's Matrix Travels(费用流)
- poj 3422 Kaka's Matrix Travels 费用流
- 【HDU】 2448 Mining Station on the Sea 费用流
- 【POJ】 1986 Distance Queries 离线LCA
- 【POJ】 2135 Farm Tour 费用流
- 【POJ】 2516 Minimum Cost 费用流
- 【HDU】 4067 Random Maze 费用流
- 【POJ】 3422 Kaka’s Matrix Travels 费用流
- 【POJ】 3680 Intervals 离散 + 费用流
- 【POJ】 3762 The Bonus Salary! 离散 + 费用流
- Java学习之路之“我的第一个Java程序“HelloWorld””
- android 支付宝之网页支付和快捷支付
- Android: Bundle
- 分布式系统设计系列 -- 概要
- Linux 学习笔记---文件系统与LVM磁盘管理
- 如何值通过xib自定义cell