题意

题目链接

Sol

这题能想到费用流就不难做了

从S向(1, 1)连费用为0,流量为K的边

从(n, n)向T连费用为0,流量为K的边

对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次,需要拆点。

对于拆出来的每个点,在其中连两条边,一条为费用为点权,流量为1,另一条费用为0,流量为INF

相邻两个点之间连费用为0,流量为INF的边。

跑最大费用最大流即可

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 51, MAX = 1e5 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9, PI = acos(-1);
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = (x * 10 + c - '0') % mod, c = getchar();
return x * f;
}
int N, K, S = 0, T = 1e5 - 1, a[MAXN][MAXN], dis[MAX], vis[MAX], Pre[MAX], id[MAXN][MAXN][2], cnt, MaxCost;
struct Edge {
int u, v, w, f, nxt;
}E[MAX];
int head[MAX], num;
inline void add_edge(int x, int y, int w, int f) {
E[num] = (Edge) {x, y, w, f, head[x]};
head[x] = num++;
}
inline void AE(int x, int y, int w, int f) {
add_edge(x, y, w, f);
add_edge(y, x, -w, 0);
}
bool SPFA() {
queue<int> q; q.push(S);
memset(dis, -0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[S] = 0;
while(!q.empty()) {
int p = q.front(); q.pop(); vis[p] = 0;
for(int i = head[p]; ~i; i = E[i].nxt) {
int to = E[i].v;
if(E[i].f && dis[to] < dis[p] + E[i].w) {
dis[to] = dis[p] + E[i].w; Pre[to] = i;
if(!vis[to]) vis[to] = 1, q.push(to);
}
}
}
return dis[T] > -INF;
}
void F() {
int canflow = INF;
for(int i = T; i != S; i = E[Pre[i]].u) chmin(canflow, E[Pre[i]].f);
for(int i = T; i != S; i = E[Pre[i]].u) E[Pre[i]].f -= canflow, E[Pre[i] ^ 1].f += canflow;
MaxCost += canflow * dis[T];
}
void MCMF() {
while(SPFA()) F();
}
signed main() {
// freopen("a.in", "r", stdin);
memset(head, -1, sizeof(head));
N = read(); K = read();
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
a[i][j] = read(), id[i][j][0] = ++cnt, id[i][j][1] = ++cnt;
AE(S, id[1][1][0], 0, K);
AE(id[N][N][1], T, 0, K);
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
AE(id[i][j][0], id[i][j][1], a[i][j], 1);
AE(id[i][j][0], id[i][j][1], 0, INF);
if(i + 1 <= N) AE(id[i][j][1], id[i + 1][j][0], 0, INF);
if(j + 1 <= N) AE(id[i][j][1], id[i][j + 1][0], 0, INF);
}
}
MCMF();
printf("%d", MaxCost);
return 0;
}
/*
3 2
1 2 3
0 2 1
1 4 2
*/

洛谷P2045 方格取数加强版(费用流)的更多相关文章

  1. 洛谷 - P2045 - 方格取数加强版 - 费用流

    原来这种题的解法是费用流. 从一个方格的左上走到右下,最多走k次,每个数最多拿走一次. 每次走动的流量设为1,起始点拆点成限制流量k. 每个点拆成两条路,一条路限制流量1,费用为价值相反数.另一条路无 ...

  2. 洛谷 P2045 方格取数加强版【费用流】

        题目链接:https://www.luogu.org/problemnew/show/P2045 题目描述 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现 ...

  3. 洛谷P2045 方格取数加强版 最小费用流

    Code: #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...

  4. [洛谷P2045]方格取数加强版

    题目大意:有一个n*n的矩阵,每个格子有一个非负整数,规定一个人从(1,1)开始,只能往右或下走,走到(n,n)为止,并把沿途的数取走,取走后数变为0.这个人共取n次,求取得的数的最大总和. 解题思路 ...

  5. LG2045 方格取数加强版 费用流

    问题描述 LG2045 题解 费用流. 套路拆点,把\((i,j)\)拆为两个点,在这两个点之间连边:一条边流量为\(1\),费用为\(a_{i,j}\),另一条边为流量为\(INF\),费用为\(0 ...

  6. P2045 方格取数加强版

    P2045 方格取数加强版 题目描述 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格 ...

  7. 棋盘DP三连——洛谷 P1004 方格取数 &&洛谷 P1006 传纸条 &&Codevs 2853 方格游戏

    P1004 方格取数 题目描述 设有N $\times N$N×N的方格图(N $\le 9$)(N≤9),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字00.如下图所示(见样例): A ...

  8. 洛谷 P2774 方格取数问题 解题报告

    P2774 方格取数问题 题目背景 none! 题目描述 在一个有 \(m*n\) 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大. ...

  9. 洛谷 P1004 方格取数 题解

    P1004 方格取数 题目描述 设有 \(N \times N\) 的方格图 \((N \le 9)\),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字\(0\).如下图所示(见样例): ...

随机推荐

  1. JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox

    JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox <html> <head> </h ...

  2. 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 2、Practice Questions

    1.以下哪一项是正确的?(检查所有适用的) (A,D,F,G) A.  a[2] 表示第二层的激活函数值向量. B. X 是一个矩阵, 其中每一行都是一个训练示例. C. a[2] (12) 表示第二 ...

  3. Java 内存分配及垃圾回收机制初探

    一.运行时内存分配 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域. 这些区域都有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有些区域则 ...

  4. CDN基本工作过程

    看了一些介绍CDN的文章,感觉这篇是讲的最清楚的. 使用CDN会极大地简化网站的系统维护工作量,网站维护人员只需将网站内容注入CDN的系统,通过CDN部署在各个物理位置的服务器进行全网分发,就可以实现 ...

  5. Android之内存泄漏

    开篇之前,我们要先理解:什么是内存泄漏.百度百科:内存泄漏(Memory Leak)是指程序中己动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等 ...

  6. 自然语言处理--jieba和gensim的分词功能

    一.jieba分词功能 1.主要模式 支持三种分词模式: 精确模式,试图将句子最精确地切开,适合文本分析: 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义: 搜索引擎模 ...

  7. FutureTask源码解析

    在Java中一般通过继承Thread类或者实现Runnable接口这两种方式来创建多线程,但是这两种方式都有个缺陷,就是不能在执行完成后获取执行的结果,因此Java 1.5之后提供了Callable和 ...

  8. CentOS7 下编译安装 Samba,什么是 SMB/CIFS 协议

    目录 一.关于 Samba 1. SMB 2. Samba 二.yum 安装 Samba 1. 安装 Samba 2. 查看版本 3. 查看配置文件 4. 启动服务 5. 本地客户端验证 6. Win ...

  9. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

  10. PHP算法------排序

    <?php/** * Created by PhpStorm. * User: 63448 * Date: 2018/5/5 * Time: 22:42 */$arr = [3,1,13,5,7 ...