题意

题目链接

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. EF6使用Mysql,踏过的那些坑

    在vs2013中使用mysql连接entityFramework经常会遇到这个问题:您的项目引用了最新实体框架:但是,找不到数据连接所需的与版本兼容的实体框架数据提供程序.请退出此向导,安装兼容提供程 ...

  2. D17——C语言基础学PYTHON

    C语言基础学习PYTHON——基础学习D17 20181014内容纲要: 1.jQuery介绍 2.jQuery功能介绍 (1)jQuery的引入方式 (2)选择器 (3)筛选 (4)文本操作 (5) ...

  3. 课程一(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) 表示第二 ...

  4. 剑指offer十六之合并两个排序的链表

    一.题目 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 二.思路 注:链表1和链表2是两个递增排序的链表,合并这两个链表得到升序链表为链表3. 首先分析 ...

  5. 继承extends、super、this、方法重写overiding、final、代码块_DAY08

    1:Math类的随机数(掌握) 类名调用静态方法.  包:java.lang 类:Math 方法:public static double random(): Java.lang包下的类是不用导包就可 ...

  6. (转)pathlib路径库使用详解

    原文:https://xin053.github.io/2016/07/03/pathlib%E8%B7%AF%E5%BE%84%E5%BA%93%E4%BD%BF%E7%94%A8%E8%AF%A6 ...

  7. 2014.10.5 再次学习LINUX

    mesg 发送信息给root y n write/talk 写消息给 wall 给所有用户发送消息 ps -aux ps -elF pstree 命令行跳转:CTRL+a行首 CTRL+e行尾 CTR ...

  8. JavaScript初探一

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. 08 - JavaSE之IO流

    IO流 JAVA流式输入输出原理:可以想象成一根管道怼到文件上,另一端是我们程序,然后流的输入输出都是按照程序本身作为第一人称说明的.比如 input,对于我们程序来说就是有数据输入我们程序,outp ...

  10. mysql 导出 sql的执行结果到 csv文件

    需求: 1. 执行某 SQL 的结果: 2. 将结果导出到 csv文件: 3. 通过命令行执行: mysql ;" | sed 's/\t/","/g;s/^/" ...