【poj3422】 Kaka's Matrix Travels
http://poj.org/problem?id=3422 (题目链接)
题意
N*N的方格,每个格子中有一个数,寻找从(1,1)走到(N,N)的K条路径,使得取到的数的和最大。
Solution
代码
// poj3422
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
struct edge {int from,to,next,c,w;}e[maxn<<1];
int head[maxn],dis[maxn],vis[maxn],f[maxn],p[maxn];
int cnt=1,n,m,es,et,K; void link(int u,int v,int c,int w) {
e[++cnt]=(edge){u,v,head[u],c,w};head[u]=cnt;
e[++cnt]=(edge){v,u,head[v],-c,0};head[v]=cnt;
}
int SPFA() {
queue<int> q;
memset(dis,-1,sizeof(dis));
q.push(es);dis[es]=0;f[es]=inf;
while (!q.empty()) {
int x=q.front();q.pop();
vis[x]=0;
for (int i=head[x];i;i=e[i].next) if (e[i].w && dis[e[i].to]<dis[x]+e[i].c) {
dis[e[i].to]=dis[x]+e[i].c;
f[e[i].to]=min(f[x],e[i].w);
p[e[i].to]=i;
if (!vis[e[i].to]) q.push(e[i].to),vis[e[i].to]=1;
}
}
if (dis[et]==-1) return 0;
for (int i=p[et];i;i=p[e[i].from]) e[i].w-=f[et],e[i^1].w+=f[et];
return f[et]*dis[et];
}
int EK() {
int ans=0;
for (int i=1;i<=K;i++) ans+=SPFA();
return ans;
}
int main() {
scanf("%d%d",&n,&K);
es=n*n+1;et=n*n+2;
for (int i=1;i<=n;i++)
for (int x,y,w,j=1;j<=n;j++) {
scanf("%d",&w);
x=(i-1)*n+j;y=x+n*n+2;
link(x,y,w,1);link(x,y,0,inf);
if (i<n) link(y,x+n,0,inf);
if (j<n) link(y,x+1,0,inf);
}
link(es,1,0,inf);link(n*n+n*n+2,et,0,inf);
printf("%d",EK());
return 0;
}
【poj3422】 Kaka's Matrix Travels的更多相关文章
- POJ3422:Kaka's Matrix Travels——题解
http://poj.org/problem?id=3422 题目大意: 从左上角走到右下角,中途取数(数>=0),然后该点的数变为0,求走k的总价值和最大值. ———————————————— ...
- POJ3422 Kaka's Matrix Travels 【费用流】*
POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...
- POJ3422 Kaka's Matrix Travels[费用流]
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9522 Accepted: ...
- poj3422 Kaka's Matrix Travels(最小费用最大流问题)
/* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...
- POJ 3422 Kaka's Matrix Travels
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9567 Accepted: ...
- POJ 3422 Kaka's Matrix Travels(费用流)
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6792 Accepted: ...
- 【题解】Sonya and Matrix Beauty [Codeforces1080E]
[题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...
- POJ3422 Kaka's Matrix Travels
描述 On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels wi ...
- 【RS】Sparse Probabilistic Matrix Factorization by Laplace Distribution for Collaborative Filtering - 基于拉普拉斯分布的稀疏概率矩阵分解协同过滤
[论文标题]Sparse Probabilistic Matrix Factorization by Laplace Distribution for Collaborative Filtering ...
随机推荐
- 窗口 - dialog - 与后端交互
与后端交互,一般需要提交表单数据,所以,这次渲染得dialog其实是一个<form> <form id="loginForm"> <table ali ...
- MathType 公式后的空格问题
注册表编辑器修改 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Design Science\DSMT6\WordC ...
- 002商城项目:maven工程的测试以及svn的使用
我们上一篇文章搭建了maven工程,这一篇文章我们就要测试这个工程. 1: 由于这个工程还没有页面,我们要首先建立一个页面.在建立页面的jsp的过程中,我发现了一个问题,我这个eclipse由于缺少J ...
- SQL Server 查询性能优化 相关文章
来自: SQL Server 查询性能优化——堆表.碎片与索引(一) SQL Server 查询性能优化——堆表.碎片与索引(二) SQL Server 查询性能优化——覆盖索引(一) SQL Ser ...
- usb驱动开发23之驱动生命线
关于字符串描述符的地位仅次于设备/配置/接口/端点四大描述符,那四大设备必须得支持,而字符串描述符对设备来说则是可选的,这并不是就说字符串描述符不重要,对咱们来说,提供字符串描述符的设备要比没有提供的 ...
- Unity 使用快速教程
Unity是微软在CodePlex上的一个开源项目,可用于依赖注入.控制反转,类似Spring,下面是使用示例: 1.先来定义几个接口.类 namespace UnityTest { public i ...
- Eclipse设置和必装插件
文章长期更新,主要是记录Eclipse好用的插件和规范的设置 插件篇: 1. StartExplorer. 在Eclipse内定位项目目录,和打开项目目录下的命令行,总是非常麻烦.有了这个插件以后,这 ...
- opencv1-安装及资料
本科用过opencv2..3.1版本,当时按照 http://wiki.opencv.org.cn/index.php/首页 上面的步骤安装的,而且使用的是IplImage和CvMat等C接口的的AP ...
- 在线程中调用SaveFileDialog
在多线程编程中,有时候可能需要在单独线程中执行某些操作.例如,调用SaveFileDialog类保存文件.首先,我们在Main方法中创建了一个新线程,并将其指向要执行的委托SaveFileAsyn.在 ...
- 曾经post为何只能通过form来提交
当我们web程序的前台,需要有数据向后台发送时候,我们第一时间想到的就是,给我们所需要提交的用户名,密码之类的数据封装到一个<form>表单里面去,而封装完毕之后,我们需要给form的提交 ...