题意

题目链接

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. Linux的基本操作

    1.linux系统的基本命令 ls  查看当前所在文夹下的内容pwd  查看当前所在的位置cd  打开文件目录touch  创建文件, 如果文件不存在, 就创建新的文件mkdir 创建文件夹rm  删 ...

  2. 彻底弄懂“PKIX path building failed”问题

    SSL的基础知识 SSL的全称是Secure Socket Layer.它的通信流程如下图所示,客户端与服务端会通过几次通信,通过非对称加密创建出一个加密密钥,用于以后的对称信息加密. 1,客户端明文 ...

  3. redis常用命令(一)

    一.redis常见的数据操作命令 http://redisdoc.com/ 二.键(key) keys *: 查询所有的key. exists key:判断某个key是否存在. move key db ...

  4. 传染病传播模型(SIS)Matlab代码

    function spreadingability=sir(A,beta,mu) for i=1:length(A) for N=1:50%随机次数 InitialState=zeros(length ...

  5. One difference between AngularJS' $location and window.location

    Recenently, I encountered a problem. Client side code is: $http({ url: "/api/runtimelicense&quo ...

  6. SpringSecurity学习之快速上手

    互联网项目中,安全与权限控制是不可回避的问题,为了解决这一些列问题,许多安全框架应运而生了.这些框架旨在帮我们解决公用的安全问题,让我们的程序更加健壮,从而让程序员全身心投入到业务开发当中.那么Spr ...

  7. This Gradle plugin requires Studio 3.0 minimum

    从github上下载的项目遇到一个问题:Error:This Gradle plugin requires Studio 3.0 minimum 意思就是说studio版本不高,导入的项目的版本是3. ...

  8. vue代码上传服务器后背景图片404解决方法

    问题:代码上传服务器后,图片404,使用的font-awesome图标也是404 解决办法: 如果你用了vue-cil,那么在build目录下找到utils.js中的ExtractTextPlugin ...

  9. mysql笔记-索引

    什么是聚簇索引 聚簇索引:索引的叶节点就是数据节点(索引值).而非聚簇索引的叶节点仍然是索引节点(告诉你怎么在表中查找这一记录),只不过有一个指针指向对应的数据块. Innodb和MyIsam区别 转 ...

  10. tomcat 调优-生产环境必备

    目录 1. tomcat 启动慢 1.1 tomcat 获取随机值阻塞 1.2 tomcat 需要部署的web应用程序太多 1.3 tomcat启动内存不足 2 Connector 调优 2.2 Co ...