POJ3422或洛谷2045 Kaka's Matrix Travels
POJ原题链接
洛谷原题链接
很裸的费用流。
将每个点\(x\)拆成\(x_1,x_2\),并从\(x_1\)向\(x_2\)连一条容量为\(1\),费用为该点的权值的边,以及一条容量为\(+\infty\),费用为\(0\)的边。
设\(x\)下方的点为\(y\),右边的点为\(z\)(如果存在),则从\(x_2\)向\(y_1,z_1\)连一条容量为\(+\infty\),费用为\(0\)的边。
最后由源点向原坐标为\((1,1)\)的点连一条容量为\(k\),费用为\(0\)的边,由原坐标为\((n,n)\)的点向汇点连一条容量为\(k\),费用为\(0\)的边。
然后跑最大费用最大流即可。
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int fi[N], di[M], da[M], ne[M], co[M], la[M], cn[M], q[M << 2], dis[N], l = 1, st, ed, n;
bool v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y, int z, int c)
{
di[++l] = y;
da[l] = z;
co[l] = c;
ne[l] = fi[x];
fi[x] = l;
di[++l] = x;
da[l] = 0;
co[l] = -c;
ne[l] = fi[y];
fi[y] = l;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
inline int ch(int x, int y)
{
return (x - 1) * n + y;
}
bool spfa()
{
int head = 0, tail = 1, i, x, y;
memset(dis, 250, sizeof(dis));
dis[st] = 0;
q[1] = st;
while (head ^ tail)
{
x = q[++head];
v[x] = 0;
for (i = fi[x]; i; i = ne[i])
if (dis[y = di[i]] < dis[x] + co[i] && da[i] > 0)
{
dis[y] = dis[x] + co[i];
la[y] = x;
cn[y] = i;
if (!v[y])
{
v[y] = 1;
q[++tail] = y;
}
}
}
return dis[ed] > -1e7;
}
int main()
{
int i, j, m, x, y, mi, s = 0, o;
n = re();
m = re();
st = (o = n * n) << 1 | 1;
ed = st + 1;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
x = re();
y = ch(i, j);
add(y, y + o, 1, x);
add(y, y + o, 1e9, 0);
if (i < n)
add(y + o, ch(i + 1, j), 1e9, 0);
if (j < n)
add(y + o, ch(i, j + 1), 1e9, 0);
}
add(st, 1, m, 0);
add(st ^ 1, ed, m, 0);
while (spfa())
{
mi = 1e9;
for (i = ed; i ^ st; i = la[i])
mi = minn(mi, da[cn[i]]);
s += mi * dis[ed];
for (i = ed; i ^ st; i = la[i])
{
da[cn[i]] -= mi;
da[cn[i] ^ 1] += mi;
}
}
printf("%d", s);
return 0;
}
POJ3422或洛谷2045 Kaka's Matrix Travels的更多相关文章
- poj3422 Kaka's Matrix Travels(最小费用最大流问题)
/* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...
- 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: ...
- 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: ...
- POJ3422 Kaka's Matrix Travels
描述 On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels wi ...
- 【poj3422】 Kaka's Matrix Travels
http://poj.org/problem?id=3422 (题目链接) 题意 N*N的方格,每个格子中有一个数,寻找从(1,1)走到(N,N)的K条路径,使得取到的数的和最大. Solution ...
- POJ3422:Kaka's Matrix Travels——题解
http://poj.org/problem?id=3422 题目大意: 从左上角走到右下角,中途取数(数>=0),然后该点的数变为0,求走k的总价值和最大值. ———————————————— ...
- POJ 3422 Kaka's Matrix Travels 【最小费用最大流】
题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...
随机推荐
- 一些常见的js问题总结
- js判断是否为undefined
typeof(isadmin)=="undefined"需要使用typeof才能判断
- soft selective sweeps 下的群体进化
1.Hard and soft selective sweeps 长期以来,快速适应主要与选择作用于高度多基因的数量性状有关,例如在育种试验期间.这些性状可以通过对大量已经存在的多态性的群体频率的微小 ...
- java.lang.IllegalAccessError: tried to access method
java.lang.IllegalAccessException: access to method denied 06-23 16:12:39.128 1253-1253/com.hbjyjt.oa ...
- Eclipse git 冲突合并
Eclipse有一个git的插件叫EGit,用于实现本地代码和远程代码对比.合并以及提交.但是在本地代码和远程代码有冲突的时候,EGit的处理方案还是有点复杂.今天就彻底把这些步骤给理清楚,并公开让一 ...
- aspectj ----- 简介
一.为什么写这个系列的博客 Aspectj一个易用的.功能强大的aop编程语言.其官网地址是:http://www.eclipse.org/aspectj/,目前最新版本为:1.7.0 RC1.但 ...
- linux上单网卡配置使用多个IP地址
准备一台红帽系列的linux(例如rhel.red hat.centos.fredora等) 方法/步骤 新建配置文件. 首先说明一下规则: 新建配置文件,配置文件名称为ifcfg-适配器名称:0-2 ...
- VM虚拟机 Centos7 lnmp环境 配置域名问题 windows浏览器访问的问题
CentOS7 ip地址为 192.168.0.155 搭有 LNMP集成环境 执行 lnmp vhost add 配置服务器 指定目录的域名 mark.com 这时windows 机器的 浏览器想 ...
- TP3.23 与Laypage 结合进行分页
demo地址:http://tp.ytlwin.top 控制器 <?php namespace Home\Controller; use Think\Controller; class Inde ...
- vue / js使用video获取视频时长
项目中遇到上传视频功能,需要有预览和获取视频时长功能,因之前使用upload(有需要的话可以参考下我之前的文章),这里就不赘述,直接用来上传视频,不过在上传之前和上传成功后的钩子里,获取不到时长: 没 ...