洛谷P2045 方格取数加强版(费用流)
题意
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 方格取数加强版(费用流)的更多相关文章
- 洛谷 - P2045 - 方格取数加强版 - 费用流
原来这种题的解法是费用流. 从一个方格的左上走到右下,最多走k次,每个数最多拿走一次. 每次走动的流量设为1,起始点拆点成限制流量k. 每个点拆成两条路,一条路限制流量1,费用为价值相反数.另一条路无 ...
- 洛谷 P2045 方格取数加强版【费用流】
题目链接:https://www.luogu.org/problemnew/show/P2045 题目描述 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现 ...
- 洛谷P2045 方格取数加强版 最小费用流
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...
- [洛谷P2045]方格取数加强版
题目大意:有一个n*n的矩阵,每个格子有一个非负整数,规定一个人从(1,1)开始,只能往右或下走,走到(n,n)为止,并把沿途的数取走,取走后数变为0.这个人共取n次,求取得的数的最大总和. 解题思路 ...
- LG2045 方格取数加强版 费用流
问题描述 LG2045 题解 费用流. 套路拆点,把\((i,j)\)拆为两个点,在这两个点之间连边:一条边流量为\(1\),费用为\(a_{i,j}\),另一条边为流量为\(INF\),费用为\(0 ...
- P2045 方格取数加强版
P2045 方格取数加强版 题目描述 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格 ...
- 棋盘DP三连——洛谷 P1004 方格取数 &&洛谷 P1006 传纸条 &&Codevs 2853 方格游戏
P1004 方格取数 题目描述 设有N $\times N$N×N的方格图(N $\le 9$)(N≤9),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字00.如下图所示(见样例): A ...
- 洛谷 P2774 方格取数问题 解题报告
P2774 方格取数问题 题目背景 none! 题目描述 在一个有 \(m*n\) 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大. ...
- 洛谷 P1004 方格取数 题解
P1004 方格取数 题目描述 设有 \(N \times N\) 的方格图 \((N \le 9)\),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字\(0\).如下图所示(见样例): ...
随机推荐
- nginx常用的超时配置说明
client_header_timeout 语法 client_header_timeout time默认值 60s上下文 http server说明 指定等待client发送一个请求头的超时时间(例 ...
- 调用notify()后,当前线程执行完synchronized块中的所有代码才会释放锁
package com.pinnet.test; public class Demo { public static void main(String[] args) { Demo demo = ne ...
- 【LeetCode】128. 最长连续序列
题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...
- 同时绑定onpropertychange 和 oninput 事件,实时检测 input、textarea输入改变事件,支持低版本IE,支持复制粘贴
实时检测 input.textarea输入改变事件,支持低版本IE,支持复制粘贴 检测input.textarea输入改变事件有以下几种: 1.onkeyup/onkeydown 捕获用户键盘输入事件 ...
- spring cloud开发、部署注意
一.开发时,配置服务的配置使用本地路径,不使用svn和git,因为后者每个开发人员都会修改配置,导致别人也拿到其他人修改的配置,本地配置示例如下: spring: application: name: ...
- Git基本命令学习
Git是一个由林纳斯·托瓦兹为了更好地管理linux内核开发而创立的分布式版本控制/软件配置管理软件,如今已经超越CVS.SVN称为主流的版本控制器.许多著名的开源项目都用Git管理,比较火的托管服务 ...
- 摆脱定时任务的cron表达式的困扰
一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...
- java Queue的用法
https://www.cnblogs.com/caozengling/p/5307992.html https://blog.csdn.net/a724888/article/details/802 ...
- postgresql逻辑结构--用户及权限管理(七)
一.用户和角色 二.创建用户和角色 三.权限管理 四.
- Ceph/共享存储 汇总
Ceph 存储集群 - 搭建存储集群 Ceph 存储集群 - 存储池 Ceph 块设备 - 命令,快照,镜像 Ceph 块设备 - 块设备快速入门 OpenStack 对接 Ceph CentOS7 ...