【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 ...
随机推荐
- IIS 伪静态配置(安装ISAPI_Rewrite配置)
第一:首先到官方网站下载ISAPI_Rewrite 我的机子是32位的就下32位免费版的,链接地址如下: http://www.helicontech.com/download/isapi_rewri ...
- BZOJ 2301 【HAOI2011】 Problem b
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- Linux shell运算符
双引号 --使用双引号可以引用除了字符$,`(单反号),\(反斜杠)外的任意字符或者字符串 --echo "参数的个数是$#" 单引号 --单引号与双引号类似,不同的是shell会 ...
- 链路层的简介和MTU
链路层杂谈(凭个人理解瞎说的,欢迎拍砖) 链路层,说白了就是把网络层的IP数据处理一下,加点东西,放到物理层上去. 加的东西:源.目的地址和CRC校验值,有的还有类型这个字段,用来区分协议. ...
- md5的C++实现
一.原理 前一阵子,想知道md5的原理查了一下资料,说得基本都一样,最后让我看懂的是这两个链接: http://blog.csdn.net/qf_study/article/details/26309 ...
- 通过js动态生成页面表格
var redlineTemplateP = $(".redlineDataList"); for (var index in detailArraryLists.rows){ v ...
- ASP.NET - SqlSugar ORM框架 更新列表
以后SqlSugar所有更新都会在这个贴子更新 SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSug ...
- win8/8.1 免密码登录设置
http://www.ehow.com/how_8013338_start-windows-password.html 原文见上方链接 1,win+r调出命令输入窗口,输入 netplwiz 回车 2 ...
- mysql 根据字段重复 删除 保留一条
delete from TableName where id not in (select minid from (select min(id) as minid from TableName gro ...
- C# Cut Line Bressenham Algorithm
using System; using System.Drawing; using System.Windows.Forms; namespace CutLine { static class Pro ...