POJ 3422 Kaka's Matrix Travels(费用流)
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
Sample Output
Source POJ Monthly--2007.10.06, Huang, Jinsong
|
测试一下最小费用最大流的模板。
很经典的建图方法。
- #include <stdio.h>
- #include <algorithm>
- #include <string.h>
- #include <iostream>
- #include <string>
- #include <queue>
- using namespace std;
- const int MAXN = ;
- const int MAXM = ;
- const int INF = 0x3f3f3f3f;
- struct Edge
- {
- int to,next,cap,flow,cost;
- }edge[MAXM];
- int head[MAXN],tol;
- int pre[MAXN],dis[MAXN];
- bool vis[MAXN];
- int N;//节点总个数,节点编号从0~N-1
- void init(int n)
- {
- N = 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[][];
- 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 3422 Kaka's Matrix Travels(费用流)的更多相关文章
- poj 3422 Kaka's Matrix Travels 费用流
题目链接 给一个n*n的矩阵, 从左上角出发, 走到右下角, 然后在返回左上角,这样算两次. 一共重复k次, 每个格子有值, 问能够取得的最大值是多少, 一个格子的值只能取一次, 取完后变为0. 费用 ...
- POJ 3422 Kaka's Matrix Travels
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9567 Accepted: ...
- POJ3422 Kaka's Matrix Travels[费用流]
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9522 Accepted: ...
- POJ 3422 Kaka's Matrix Travels (K取方格数:最大费用流)
题意 给出一个n*n大小的矩阵,要求从左上角走到右下角,每次只能向下走或者向右走并取数,某位置取过数之后就只为数值0,现在求解从左上角到右下角走K次的最大值. 思路 经典的费用流模型:K取方格数. 构 ...
- [poj] 3422 Kaka's Matrix Travels || 最小费用最大流
原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...
- POJ 3422 Kaka's Matrix Travels K取方格数
题目:给出n*n的方格矩阵,现在从左上方走m次到右下方,问m次能够获得的最大价值和. 分析:最大费用流.拆点进行限制每个格子只取一次,假设点x拆成 x,xx,右边(假设有)y,yy,下方(假设有)z, ...
- POJ 3422 Kaka's Matrix Travels 【最小费用最大流】
题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...
- POJ 3422 Kaka's Matrix Travels(最小费用最大流)
http://poj.org/problem?id=3422 题意 : 给你一个N*N的方格,每个格子有一个数字,让你从左上角开始走,只能往下往右走,走过的数字变为0,走K次,问最大能是多大,累加的. ...
- POJ 3422 Kaka's Matrix Travels(拆点+最大费用流)题解
题意:小A从左上角走到右下角,每个格子都有一个价值,经过这个格子就把价值拿走,每次只能往下或往右走,问你走k次最多能拿多少价值的东西. 思路:这里有一个限制条件就是经过之后要把东西拿走,也就是每一格的 ...
随机推荐
- A Tutorial on Network Embeddings
A Tutorial on Network Embeddings paper:https://arxiv.org/abs/1808.02590 NE 的中心思想就是找到一种映射函数,该函数将网络中 ...
- 小白成长记-----python实现注册的小程序
# 3.写一个注册的程序,输入username,密码,# 密码确认,输入的账号和密码不能为空,两次输入密码必须一致,# 用户名不能重复,错误次数四次 .注册成功提示成功# 把注册账号密码信息的写到文件 ...
- [PAT] 1141 PAT Ranking of Institutions(25 分)
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- mac下安装golang
1.安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...
- [译]怎样用HTML5 Canvas制作一个简单的游戏
这是我翻译自LostDecadeGames主页的一篇文章,原文地址:How To Make A Simple HTML5 Canvas Game. 下面是正文: 自从我制作了一些HTML5游戏(例如C ...
- ibatis的动态Mapped Statement 标签
动态Mapped Statement 直接使用JDBC 一个非常普遍的问题是动态SQL.使用参数值.参数本身和数据列都是动态的SQL,通常非常困难.典型的解决方法是,使用一系列if-else 条件语句 ...
- angularjs 微信授权登录 微信支付
最近做一个项目,用angular 一个单页应用,打算打包成 跨平台移动App 以及在微信里面使用.给大家一个案例 首先,熟悉一下微信授权部分的源代码,如下所示: javascript 前端代码: va ...
- hdu5782
官方题解不是很详细 首先有一个结论:若A=pa+sa B=pb+sb A.B串循环同构,则可以构造一个可行方案(pa,sb) (sa,pb)中有一个是最长匹配,这个不难用反证法证明. 对于s1,s2串 ...
- hbase+hadoop+hdfs集群搭建 集成spring
序言 最近公司一个汽车项目想用hbase做存储,然后就有了这篇文字,来,来,来, 带你一起征服hbase,并推荐一本书<hbase权威指南> 这是一本极好的hbase入门书籍,我花了一个晚 ...
- 转:LLVM与Clang的概述及关系
转:http://www.cnblogs.com/saintlas/p/5738739.html LLVM是构架编译器(compiler)的框架系统,以C++编写而成,用于优化以任意程序语言 ...