POJ 3422Kaka'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(最小费用最大流)的更多相关文章
- [poj] 3422 Kaka's Matrix Travels || 最小费用最大流
原题 给一个N*N的方阵,从[1,1]到[n,n]走K次,走过每个方格加上上面的数,然后这个格上面的数变为0.求可取得的最大的值. 要求最大值,所以把边权全为负跑最小费用即可.因为只有第一次经过该点的 ...
- poj3422 Kaka's Matrix Travels(最小费用最大流问题)
/* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...
- POJ 2195 Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意 : N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 2135 Farm Tour(最小费用最大流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- POJ 2516 Minimum Cost (最小费用最大流)
POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...
- POJ 2195 Going Home 【最小费用最大流】
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Total Submissions:2715 ...
- POJ - 2516 Minimum Cost(最小费用最大流)
1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...
- POJ 2135 Farm Tour(最小费用最大流,变形)
题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...
随机推荐
- HDU5478 原根求解
看别人做的很简单我也不知道是怎么写出来的 自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b那么只要求出幂a,b就能得到所求值a,b 自己慢慢化简就会发现可以抵消n然 ...
- 虚拟机安装Centos版本的linux
选择自定义安装,然后一路确定下去,对了,Centos的版本一定要高,比如centos 6,太低安装会出问题
- HDFS权限问题
HDFS权限问题 Win下Eclipse提交hadoop程序出错:org.apache.hadoop.security.AccessControlException: Permission denie ...
- 大嫂的HTML
<html> <head> <style type="text/css"> *{ margin: 0; padding: 0; ...
- javascript prototype 剖析
学过javascript的一定对prototype不陌生,但是这个究竟是个什么东西,就不一定很清楚. 我们先对prototype进行一个定义:每个函数都有一个prototype属性,这个属性是指向一个 ...
- Java运算符及顺序、选择结构
:运算符(掌握) ()算术运算符 A:+,-,*,/,%,++,-- B:+的用法 a:加法 b:正号 c:字符串连接符 C:/和%的区别 数据做除法操作的时候,/取得是商,%取得是余数 D:++和- ...
- 深入C#数据类型小部分第二章
值类型和引用类型C#的值类型包括:结构体(数值类型,bool型,用户定义的结构体),枚举,可空类型. C#的引用类型包括:数组,用户定义的类.接口.委托,object,字符串. 数组的元素,不管是引用 ...
- poj1837 dp
//Accepted 2176 KB 47 ms //杠杆平横的条件:sum(c[i]*sum(g[j]))=0 // 所有的hook到原点的距离乘它上面挂着的物体的重量和的和为0 //对于一个hoo ...
- poj3356 dp
//Accepted 4100 KB 0 ms //类似poj1080 //dp[i][j]表示s1用前i个,s2用前j个的最少匹配步数 //dp[i][j]=min(dp[i][j-1]+1,dp[ ...
- cometd的js端代码
一:js端使用方式 CometD JavaScript的配置.整个API可以通过一个单一的原型名为org.cometd.Cometd的对象来调用.Dojo工具包中有一个名称为dojox.cometd的 ...