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次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...
随机推荐
- 微信小程序--分享报错(thirdScriptError Cannot read property 'from' of undefined;at pages/index/index page onShareAppMessage function TypeError: Cannot read property 'from' of undefined)
分享功能: onShareAppMessage: function (res) { if (res.from === 'button') { // 来自页面内转发按钮 console.log(res. ...
- the type java.io.ObjectInputStream cannot be resolved. It is indirectly......
问题的原因: 配置tomcat7.0的时候自己设置了jre的版本1.8,而没有用myeclipse10自带的jre1.6,导致了出现了差错! 两种解决的办法: 1.点击windows--->pr ...
- 批量更新list<string,string>
public void UpdateList(List<MysqlModule.Model.pro_premanifest> modelList) { List<MySqlParam ...
- CRM某些表加入审计
--更新参数文件,设置Audit等级 alter system set audit_trail=db,extended scope=spfile; --更新参数文件,开始Audit alter sys ...
- js 中的原型prototype
每次创建新函数,就会根据规则为该函数创建一个 prototype 属性,该属性是一个指向函数原型对象的指针.并且原型对象都默认拥有一个 constructor 属性,该属性是一个指向那个新建函数的指针 ...
- IMPDP NETWORK_LINK参数
在<[IMPDP]同一数据库实例不同用户间数据迁移复制—— NETWORK_LINK参数>(http://space.itpub.net/519536/viewspace-631571)文 ...
- LibreOJ 6277. 数列分块入门 1
题目链接:https://loj.ac/problem/6277 参考博客:https://www.cnblogs.com/stxy-ferryman/p/8547731.html 两个操作,区间增加 ...
- Unity3D研究院之设置自动旋转屏幕默认旋转方向
如下图所示,在处理屏幕默认旋转方向的时候可以在这里进行选择,上下左右一共是4个方向. 策划的需求是游戏采用横屏,但是要求支持两个方向自动旋转,如下图所示,我的设置是这样的. Default Orien ...
- JSON Extractor
JMeter处理responses 的json 对于请求1返回的结果,处理以后作为请求2的参数,JMeter提供了JSON 提取器 比如 responses 返回: {"statusCode ...
- mybatis批量更新update-设置多个字段值 报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
mybatis批量更新update-设置多个字段值 2016年08月01日 12:49:26 姚一号 阅读数:29539 标签: mysql mybatis批量更新批量更新allowMultiQuer ...