Problem Statement

We have a grid with $H$ rows and $W$ columns. Each square is painted either white or black.
For each integer pair $(i, j)$ such that $1 \leq i \leq H$ and $1 \leq j \leq W$,
the color of the square at the $i$-th row from the top and $j$-th column from the left (we simply denote this square by Square $(i, j)$) is represented by $A_{i, j}$.
Square $(i, j)$ is white if $A_{i, j} = 0$, and black if $A_{i, j} = 1$.

You may perform the following operations any number of (possibly $0$) times in any order:

  • Choose an integer $i$ such that $1 \leq i \leq H$, pay $R_i$ yen (the currency in Japan), and invert the color of each square in the $i$-th row from the top in the grid. (White squares are painted black, and black squares are painted white.)
  • Choose an integer $j$ such that $1 \leq j \leq W$, pay $C_j$ yen, and invert the color of each square in the $j$-th column from the left in the grid.

Print the minimum total cost to satisfy the following condition:

  • There exists a path from Square $(1, 1)$ to Square $(H, W)$
    that can be obtained by repeatedly moving down or to the right, such that all squares in the path (including Square $(1, 1)$ and Square $(H, W)$) have the same color.

We can prove that it is always possible to satisfy the condition in a finite number of operations under the Constraints of this problem.

Constraints

  • $2 \leq H, W \leq 2000$
  • $1 \leq R_i \leq 10^9$
  • $1 \leq C_j \leq 10^9$
  • $A_{i, j} \in \lbrace 0, 1\rbrace$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$H$ $W$
$R_1$ $R_2$ $\ldots$ $R_H$
$C_1$ $C_2$ $\ldots$ $C_W$
$A_{1, 1}A_{1, 2}\ldots A_{1, W}$
$A_{2, 1}A_{2, 2}\ldots A_{2, W}$
$\vdots$
$A_{H, 1}A_{H, 2}\ldots A_{H, W}$

Output

Print the answer.


Sample Input 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

Sample Output 1

9

We denote a white square by 0 and a black square by 1.
On the initial grid, you can pay $R_2 = 3$ yen to invert the color of each square in the $2$-nd row from the top to make the grid:

0100
0100
1010

Then, you can pay $C_2 = 6$ yen to invert the color of each square in the $2$-nd row from the left to make the grid:

0000
0000
1110

Now, there exists a path from Square $(1, 1)$ to Square $(3, 4)$ such that all squares in the path have the same color (such as the path $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4)$).
The total cost paid is $3+6 = 9$ yen, which is the minimum possible.


Sample Input 2

15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000

Sample Output 2

125

思考如何走出这条路径。

如果我现在是往右走的,那么如果现在颜色不对,我可以把这一列给修改了。只要我不转成往下走,修改这一列是不会有影响别的格子的。往下走时同理。所以题目说了一定有解,而我们构造出了一个可行解。

但是我们还可以往右走时修改这一行,那么现在有两个影响因素。定义 \(dp_{i,j,k,l}\) 表示走到了点 \((i,j)\),原来方向为 \(k\), \(l\) 表示在这个方向上是否经过反转。

那么现在有可能把整个棋盘变成 \(0\) 或者 \(1\),这不妨分开考虑。观察这一位是否需要经过整行/整列反转,计算代价。同时看要不要转弯,转弯后是否经过反转。

注意一件事,走在 \((1,1)\) 时是可以决定行反转还是列反转的,都要试一下。

感觉写成记忆化搜索直接点

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2005;
int h,w,r[N],c[N],a[N][N];
LL dp[N][N][2][2],ans;//0表示下,1表示右
LL dfs(int x,int y,int i,int j)
{
if(dp[x][y][i][j]!=-1)
return dp[x][y][i][j];
LL ret=1e18;
if(i)
{
int c=a[x][y]^j? ::c[y]:0;
if(x==h&&y==w)
return c;
if(y<w)
ret=min(ret,dfs(x,y+1,i,j)+c);
if(x<h)
{
if(a[x][y]^j)
ret=min(ret,dfs(x+1,y,0,1)+c);
else
ret=min(ret,dfs(x+1,y,0,0));
}
}
else
{
int c=a[x][y]^j? r[x]:0;
if(x==h&&y==w)
return c;
if(x<h)
ret=min(ret,dfs(x+1,y,i,j)+c);
if(y<w)
{
if(a[x][y]^j)
ret=min(ret,dfs(x,y+1,1,1)+c);
else
ret=min(ret,dfs(x,y+1,1,0));
}
}
// printf("%d %d %d %d %lld\n",x,y,i,j,ret);
return dp[x][y][i][j]=ret;
}
int main()
{
scanf("%d%d",&h,&w);
for(int i=1;i<=h;i++)
scanf("%d",r+i);
for(int i=1;i<=w;i++)
scanf("%d",c+i);
for(int i=1;i<=h;i++)
{
char ch;
for(int j=1;j<=w;j++)
scanf(" %c",&ch),a[i][j]=ch-'0';
}
memset(dp,-1,sizeof(dp));
ans=min(min(dfs(1,1,0,0),dfs(1,1,1,0)),min(dfs(1,1,0,1)+c[1],dfs(1,1,1,1)+r[1]));
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
a[i][j]^=1;
memset(dp,-1,sizeof(dp));
ans=min(ans,min(min(dfs(1,1,0,0),dfs(1,1,1,0)),min(dfs(1,1,0,1)+c[1],dfs(1,1,1,1)+r[1])));
printf("%lld",ans);
}

[ABC264F] Monochromatic Path的更多相关文章

  1. NodeJs之Path

    Path模块 NodeJs提供的Path模块,使得我们可以对文件路径进行简单的操作. API var path = require('path'); var path_str = '\\Users\\ ...

  2. 【原】实时渲染中常用的几种Rendering Path

    [原]实时渲染中常用的几种Rendering Path 本文转载请注明出处 —— polobymulberry-博客园 本文为我的图形学大作业的论文部分,介绍了一些Rendering Path,比较简 ...

  3. Node.js:path、url、querystring模块

    Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...

  4. VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Thinking in Unity3D:渲染管线中的Rendering Path

      关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的 ...

  8. node之path模块

    node之path模块 原文链接 //引用该模块 var path = require("path"); 1.路径解析,得到规范化的路径格式 对window系统,目录分隔为'', ...

  9. Linux系统修改PATH环境变量方法

    在Linux安装一些软件通常要添加路径环境变量PATH.PATH环境变量通俗的讲就是把程序的路径"备案"到系统中,这样执行这些程序时就不需要输入完整路径,直接在bash输入程序名就 ...

  10. 利用XML FOR PATH 合并分组信息

    -- ================================================ -- Description:合并分组内容 -- Author:夏保华 -- Date:2009 ...

随机推荐

  1. 9、Spring之代理模式

    9.1.环境搭建 9.1.1.创建module 9.1.2.选择maven 9.1.3.设置module名称和路径 9.1.4.module初始状态 9.1.5.配置打包方式和依赖 <?xml ...

  2. 深入了解API接口技术及其应用

    尊敬的读者们,大家好!在互联网时代,API(Application Programming Interface)接口已经成为开发者们连接各种应用.获取数据的重要工具.今天,我们将深入探讨API接口技术 ...

  3. CDC一键入湖:当 Apache Hudi DeltaStreamer 遇见 Serverless Spark

    Apache Hudi的DeltaStreamer是一种以近实时方式摄取数据并写入Hudi表的工具类,它简化了流式数据入湖并存储为Hudi表的操作,自 0.10.0 版开始,Hudi又在DeltaSt ...

  4. HTML一键打包EXE工具1.9.9发布 (包含最新版下载地址)

    HTML一键打包EXE工具(HTML封装EXE,桌件)是一款能将任意HTML项目(网址)打包为单个EXE文件的工具,无需依赖浏览器和服务器,直接双击即可运行.该工具支持多种HTML项目类型,包括KRP ...

  5. [自然语言处理] 基于pycorrector实现文本纠错

    文本纠错(Text Error Correction)技术旨在自动修正输入文本中的拼写.语法.标点符号等错误,以提高文本的准确性.通顺性和规范性.该技术可以通过自然语言处理技术实现,基于上下文和语言规 ...

  6. 关于wake on lan远程唤醒主机的问题,长时间关机无法远程唤醒

    英特尔在年初发布了几款低功耗的CPU,国内厂商在迷你主机领域纷纷搭载新款CPU,卖的火爆.之前关注过迷你主机这块,于是,我也入手一个迷你主机玩玩,买的是板载N100的迷你主机.使用过程中会涉及到如何远 ...

  7. C++的extern关键字在HotSpot VM中的重要应用

    extern关键字有两个用处: (1)extern在C/C++语言中表示函数和全局变量作用范围(可见性)的关键字,这个关键字会告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用. (2)在C+ ...

  8. 3DMatch

    详细描述链接:3DMatch 数据集.github(介绍非常详细) 官网主页: 主页 3DMatch数据集收集了来自于62个场景的数据,其中54个场景的数据用于训练,8个场景的数据用于评估,其具体名称 ...

  9. Matlab 设计仿真CIC滤波器

    2023.09.26 使用CIC滤波器用于降采样.同样的,CIC滤波器也适用于升采样. 参考连接: [1] Matlab中CIC滤波器的应用_dsp.cicdecimator_张海军2013的博客-C ...

  10. css的认知与样式

      目录 1. 介绍css 2. CSS语法 3. CSS注释 4. CSS中的颜色值 5. CSS长度单位 6. html引入CSS的三种方法 6.1 行内样式(内联样式) 6.2   内嵌样式 6 ...