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

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

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

  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. POJ 3422 Kaka's Matrix Travels

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

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

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

  6. POJ3422 Kaka's Matrix Travels

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

  7. 【poj3422】 Kaka's Matrix Travels

    http://poj.org/problem?id=3422 (题目链接) 题意 N*N的方格,每个格子中有一个数,寻找从(1,1)走到(N,N)的K条路径,使得取到的数的和最大. Solution ...

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

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

  9. POJ 3422 Kaka's Matrix Travels 【最小费用最大流】

    题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...

随机推荐

  1. python 引用和对象理解(转)

    引用和对象分离 从最开始的变量开始思考: 在python中,如果要使用一个变量,不需要提前进行声明,只需要在用的时候,给这个变量赋值即可 (这个和C语言等静态类型语言不同,和python为动态类型有关 ...

  2. JS----事件3

    一 事件对象(event):与特定事件相关且包含有关该事件详细信息的对象通过事件可以触发event对象的元素,鼠标的位置及状态,按下的键等等event对象只在事件发生的过程中才有效非IE浏览器里的ev ...

  3. ubuntu下没有ping命令

    root@node2:/# apt-get install inetutils-ping

  4. MySQL 事务 隔离级别

    前两天面试,问到了四种隔离级别,当时觉得大多数数据库都为read committed,结果没想到mysql是个例外.在此做一下隔离级别和各种数据库锁的使用. 首先说一下ACID四大特性: 四大特性   ...

  5. Mybatis返回List<Map<K,V>>

    最终映射的字段名 会被作为 hashMap 的 key , <!-- TODO 测试返回 HashMap--> <resultMap id="testResultMap&q ...

  6. 使用 Actuator 监控

    参考文章:https://www.jianshu.com/p/ba85f56a2013 Actuator 提供对自身应用的监控.配置查看等. 步骤一:导入actuator 依赖 <depende ...

  7. ELK 日志学习

    一.Elasticsearch 安装(所有的版本均使用5.5.0 ,且版需要相同 logstash \ kibana \ filebeat ) 官网下载地址:https://www.elastic.c ...

  8. Android 7.0解决抓取不到https请求的问题

    问题:Android7.0系统,使用fiddler不能抓取https请求 解决方法:  1.在源码res目录下新建xml目录,增加network_security_config.xml文件 (工程名/ ...

  9. MySQL数据类型及使用场景

    MySQL数据类型介绍 整数类型 类型名称 说明 存储需求 取值范围有符号 取值范围符号 TINYINT 很小的整数 1个字节 -128~127 0-255 SMALLINT 小的整数 2个字节 32 ...

  10. jQuery权威指南(第2版) 学习一 jQuery操作DOM

    jQuery操作DOM 获取元素的属性 attr(name) 获取元素属性的语法格式如下: attr(name) 其中,参数 name 表示属性的名称. 例子: <img alt="& ...