http://poj.org/problem?id=3422 (题目链接)

题意

  N*N的方格,每个格子中有一个数,寻找从(1,1)走到(N,N)的K条路径,使得取到的数的和最大。

Solution

  同【codevs1277】 方格取数

代码

// 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的更多相关文章

  1. POJ3422:Kaka's Matrix Travels——题解

    http://poj.org/problem?id=3422 题目大意: 从左上角走到右下角,中途取数(数>=0),然后该点的数变为0,求走k的总价值和最大值. ———————————————— ...

  2. POJ3422 Kaka's Matrix Travels 【费用流】*

    POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...

  3. POJ3422 Kaka's Matrix Travels[费用流]

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9522   Accepted:  ...

  4. poj3422 Kaka's Matrix Travels(最小费用最大流问题)

    /* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...

  5. POJ 3422 Kaka's Matrix Travels

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9567   Accepted:  ...

  6. POJ 3422 Kaka's Matrix Travels(费用流)

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6792   Accepted:  ...

  7. 【题解】Sonya and Matrix Beauty [Codeforces1080E]

    [题解]Sonya and Matrix Beauty [Codeforces1080E] 传送门:\(Sonya\) \(and\) \(Matrix\) \(Beauty\) \([CF1080E ...

  8. POJ3422 Kaka's Matrix Travels

    描述 On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels wi ...

  9. 【RS】Sparse Probabilistic Matrix Factorization by Laplace Distribution for Collaborative Filtering - 基于拉普拉斯分布的稀疏概率矩阵分解协同过滤

    [论文标题]Sparse Probabilistic Matrix Factorization by Laplace Distribution for Collaborative Filtering  ...

随机推荐

  1. 未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法

    一.未在本地计算机上注册“microsoft.ACE.oledb.4.0”提供程序 http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c ...

  2. CodeGenerator.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CiCe ...

  3. 《细细品味Hive》系列课程

    Hi,博友: 我是解耀伟,笔名是虾皮,最近我在极客学院录制Hive系列教程,也是督促自己学习一种方式,可以把自己的学习积累有方向,星期天也能做点有意义的事情.在做每一期的过程中,需要找资料,总结,先自 ...

  4. 翻译qmake文档(二) Getting Started

    翻译qmake文档 目录 原英文文档: http://qt-project.org/doc/qt-5/qmake-tutorial.html         本教程教讲授qmake基础知识.这个手册里 ...

  5. Node 进阶:express 默认日志组件 morgan 从入门使用到源码剖析

    本文摘录自个人总结<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 章节概览 morgan是express默认的日志中间件, ...

  6. 处理Linux下subversion尝试连接自建的VisualSVN server报“Key usage violation in certificate has been detected”错误的问题

    在Linux下使用subversion尝试链接VisualSVN server搭建的svn库,可能会报下面错误, svn: OPTIONS of 'https://server.domain.loca ...

  7. 文本 To 音频

    文本  To  音频 TextToSpeech介绍 TextToSpeech,简称 TTS,是Android 1.6版本中比较重要的新功能.将所指定的文本转成不同语言音频输出.它可以方便的嵌入到游戏或 ...

  8. 使用rest方式修改服务端xml文件

    想法: 最近想用REST的方式去修改一个远端的xml文件,于是自己写了个Handler 设计: 用现成的XPath,和GET,POST,PUT,DELETE这4个http方法作为动作,达到直接读写xm ...

  9. 转一篇Unity的相机动画控制

    最近真是忙,连研究细看的时间都没有了,原帖地址:https://alastaira.wordpress.com/2013/11/08/smooth-unity-camera-transitions-w ...

  10. pay-as-you-go

    What is pay as you go? A pay as you go deal means you aren’t tied into a contract and can top up you ...