POJ训练计划3422_Kaka's Matrix Travels(网络流/费用流)
解题报告
题意:
从n×n的矩阵的左上角走到右下角,每次仅仅能向右和向下走,走到一个格子上加上格子的数,能够走k次。问最大的和是多少。
思路:
建图:每一个格子掰成两个点,分别叫“出点”,“入点”,
入点到出点间连一个容量1。费用为格子数的边。以及一个容量∞,费用0的边。
同一时候。一个格子的“出点”向它右、下的格子的“入点”连边。容量∞,费用0。
源点向(0,0)的入点连一个容量K的边。(N-1,N-1)的出点向汇点连一个容量inf的边。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
struct node {
int v,cost,cap,next;
} edge[101000];
int head[10000],dis[10000],pre[10000],vis[10000],f[10000],mmap[100][100],cnt,s,t,n,m,k,flow,cost;
void add(int u,int v,int cost,int cap) {
edge[cnt].v=v;
edge[cnt].cost=cost;
edge[cnt].cap=cap;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].cost=-cost;
edge[cnt].cap=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int _spfa() {
for(int i=s; i<=t; i++) {
dis[i]=-1;
pre[i]=f[i]=vis[i]=0;
}
dis[s]=0;
f[s]=inf;
pre[s]=-1;
vis[s]=1;
queue<int>Q;
Q.push(s);
while(!Q.empty()) {
int u=Q.front();
Q.pop();
vis[u]=0;
for(int i=head[u]; i!=-1; i=edge[i].next) {
int v=edge[i].v;
if(edge[i].cap&&dis[v]<dis[u]+edge[i].cost) {
dis[v]=dis[u]+edge[i].cost;
f[v]=min(f[u],edge[i].cap);
pre[v]=i;
if(!vis[v]) {
vis[v]=1;
Q.push(v);
}
}
}
}
if(dis[t]==-1)return 0;
cost+=dis[t];
flow+=f[t];
for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) {
edge[i].cap-=f[t];
edge[i^1].cap+=f[t];
}
return 1;
}
void mcmf() {
cost=flow=0;
while(_spfa());
printf("%d\n",cost);
}
int dx[]= {0,1};
int dy[]= {1,0};
int main() {
int i,j;
scanf("%d%d",&n,&k);
memset(head,-1,sizeof(head));
cnt=0;
m=n*n;
s=0;
t=2*m+1;
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
scanf("%d",&mmap[i][j]);
}
}
add(s,1,0,k);
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
add(i*n+j+1,m+i*n+j+1,mmap[i][j],1);
add(i*n+j+1,m+i*n+j+1,0,inf);
for(int l=0; l<2; l++) {
int x=i+dx[l];
int y=j+dy[l];
if(x>=0&&x<n&&y>=0&&y<n) {
add(m+i*n+j+1,x*n+y+1,0,inf);
}
}
}
}
add(2*m,t,0,inf);
mcmf();
return 0;
}
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7807 | Accepted: 3140 |
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
POJ训练计划3422_Kaka's Matrix Travels(网络流/费用流)的更多相关文章
- POJ 3422 Kaka's Matrix Travels(费用流)
POJ 3422 Kaka's Matrix Travels 题目链接 题意:一个矩阵.从左上角往右下角走k趟,每次走过数字就变成0,而且获得这个数字,要求走完之后,所获得数字之和最大 思路:有点类似 ...
- POJ 3422 Kaka's Matrix Travels (最小费用最大流)
POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...
- POJ 3422 Kaka's Matrix Travels(费用流)
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6792 Accepted: ...
- POJ3422 Kaka's Matrix Travels 【费用流】*
POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...
- poj Kaka's Matrix Travels
Kaka's Matrix Travels 题目: 给出一个矩阵.求仅仅能向下或者向右的情况下能得到的最大和.一般的是指遍历一次,而这个是能够反复走K次.每经过一次后就把该点设为0.求最大和. 算法: ...
- POJ3422 Kaka's Matrix Travels 【最大费用最大流】
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8006 Accepted: ...
- poj3422 Kaka's Matrix Travels(最小费用最大流问题)
/* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...
- 图论--网络流--费用流--POJ 2156 Minimum Cost
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- BZOJ2055 80人环游世界 网络流 费用流 有源汇有上下界的费用流
https://darkbzoj.cf/problem/2055 https://blog.csdn.net/Clove_unique/article/details/54864211 ←对有上下界费 ...
随机推荐
- (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
android res文件夹里面的drawable(ldpi.mdpi.hdpi.xhdpi.xxhdpi) (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),F ...
- Spark新愿景:让深度学习变得更加易于使用——见https://github.com/yahoo/TensorFlowOnSpark
Spark新愿景:让深度学习变得更加易于使用 转自:https://www.jianshu.com/p/07e8200b7cea 前言 Spark成功的实现了当年的承诺,让数据处理变得更容易,现在 ...
- nyoj--239--月老的难题(最小点覆盖)
月老的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘. 现在,由于一些原因,部分男孩与女孩可能结成幸福的一 ...
- hdoj--3072--Intelligence System(scc+缩点+数据去重)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- day63-webservice 09.jquery调用ajax
WebService可以有很多种调用方式,除了之前说的,还可以有jquery.拿原生的Ajax做调用,拿jquery怎么调用啊?原生的能调,jquery指定也能调.原生的Ajax是通过网页直接点HTM ...
- [HTML] 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
在做一个微信的微网站中的一个便民服务电话功能的应用,用到移动web页面中列出的电话号码,点击需要实现调用通讯录,网页一键拨号的拨打电话功能. 如果需要在移动浏览器中实现拨打电话,发送email,美国服 ...
- Oracle 11g RAC for LINUX rhel 6.X silent install(静默安装)
一.前期规划 1.硬件环境 CPU: Intel(R) Xeon(R) CPU E7-4820 v4 @ 2.00GHz 8*10核 内存:512GB OCR:2147*5 MB DATA1:2TB ...
- 树莓派-基于aria2实现离线下载
安装aria2 aria2是linux下的一个下载工具,它支持http.bt种子.磁力链接三种方式下载 sudo apt-get install aria2 配置aria2 aria2支持命令参数,也 ...
- c# 异步任务队列(可选是否使用单线程执行任务,以及自动取消任务)
使用demo,(.net framework 4.0 自行添加async wait 扩展库) class Program { static void Main(string[] args) { Con ...
- 异常及String
异常时描述错误信息的对象,在编码过程中我们会遇到很多异常 例如: 1.java.lang.ArithmeticException 算数异常.算数运算出现错误时抛出 比如用0做除数 2.java.lan ...