Kaka's Matrix Travels
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9460   Accepted: 3844

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

【分析】下面是模板。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x7fffffff
#define mod 10000
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
struct Edge {
int to,next,cap,flow,cost;
} edge[M];
int head[N],tol;
int pre[N],dis[N];
bool vis[N];
int T;//节点总个数,节点编号从0~N-1
void init(int n) {
T = n;
tol = ;
memset(head,-,sizeof(head));
}
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 = ;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = ;
edge[tol].cost = -cost;
edge[tol].flow = ;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t) {
queue<int>q;
for(int i = ; i < N; i++) {
dis[i] = inf;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; 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] == -)return false;
else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost) {
int flow = ;
cost = ;
while(spfa(s,t)) {
int Min = inf;
for(int i = pre[t]; i != -; i = pre[edge[i^].to]) {
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t]; i != -; i = pre[edge[i^].to]) {
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
} int a[N][N];
int main() {
int n,k;
while(~scanf("%d%d",&n,&k) ) {
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
scanf("%d",&a[i][j]);
init(*n*n+);
for(int i = ; i < n; i++)
for(int j = ; j < n; j++) {
addedge(n*i+j+,n*n+n*i+j+,,-a[i][j]);
addedge(n*i+j+,n*n+n*i+j+,inf,);
} for(int i = ; i < n; i++)
for(int j = ; j < n; j++) {
if(i < n-)
addedge(n*n+n*i+j+,n*(i+)+j+,inf,);
if(j < n-)
addedge(n*n+n*i+j+,n*i+j++,inf,);
}
addedge(,,k,);
addedge(*n*n,*n*n+,inf,);
int cost;
minCostMaxflow(,*n*n+,cost);
printf("%d\n",-cost);
}
return ;
}

POJ 3422Kaka's Matrix Travels(最小费用最大流)的更多相关文章

  1. [poj] 3422 Kaka's Matrix Travels || 最小费用最大流

    原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...

  2. poj3422 Kaka's Matrix Travels(最小费用最大流问题)

    /* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...

  3. POJ 2195 Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意 :  N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...

  4. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  5. POJ 2135 Farm Tour(最小费用最大流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  6. POJ 2516 Minimum Cost (最小费用最大流)

    POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...

  7. POJ 2195 Going Home 【最小费用最大流】

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:2715 ...

  8. POJ - 2516 Minimum Cost(最小费用最大流)

    1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...

  9. POJ 2135 Farm Tour(最小费用最大流,变形)

    题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...

随机推荐

  1. 如何在滚动报表时保持标题可见 (Reporting Services)

    From: https://msdn.microsoft.com/zh-cn/library/bb934257.aspx 对于跨多页的表或矩阵数据区域,可以控制滚动报表时是否始终显示包含列标题的初始行 ...

  2. POJ3624

    题目大意: 给出珠宝的重量Wi和珠宝的价值Di,并给定一个重量范围M,在不超过M的情况下求取到的珠宝的最大值,N为列出珠宝的重量. #include <iostream> #include ...

  3. 集成自动化的条形码功能到internet应用程序,网站或自定义Java应用程序的条码控件Java Barcode Package

    Java Barcode Package控件是一款条码生成控件,包含所有的JavaBean,Applets,Servlets和类库可以使用于装有Java虚拟机的任何平台,包括Windows®, Lin ...

  4. SharePoint安全 - 在Goolge和Bing中查找SharePoint相关内容

    博客地址 http://blog.csdn.net/foxdave 本篇提供两个查询串字典,分别对应Google和Bing的搜索,用来查询SharePoint网站的相关内容 Google ShareP ...

  5. lightoj1082 线段树

    //Accepted 5596 KB 396 ms //线段树求区间最小值 #include <cstdio> #include <cstring> #include < ...

  6. poj3280 区间dp

    //Accepted 15880 KB 250 ms #include <cstdio> #include <cstring> #include <iostream> ...

  7. oracle触发器如何使用2

    触发器 是特定事件出现的时候,自动执行的代码块.类似于存储过程,但是用户不能直接调用他们.触发器是许多关系数据库系统都提供的一项技术.在ORACLE系统里,触发器类似过程和函数,都有声明,执行和异常处 ...

  8. mysql存储过程笔记

    http://blog.csdn.net/wangchao0605/article/details/5935988 基本语法 创建存储过程 create procedure sp_name()begi ...

  9. 2016-1-5第一个完整APP 私人通讯录的实现 1:登录界面及跳转的简单实现2

    ---恢复内容开始--- 实际效果如上 一:Segue的学习 1.什么是Segue: Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue)   ...

  10. Reverse Integer ---- LeetCode 007

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Solution ...