CF2205F.Simons and Reconstructing His Roads
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Dropping all the time, but the exit I can find.
— SHUN, DYING FOR YOU
There are n×m crossroads in the city, named crossroad (1,1),(1,2),…,(n,m). The first number represents the row, and the second represents the column.
There exists and only exists streets between (i,j) and (i+1,j) or between (i,j) and (i,j+1). The street between (i,j) and (i+1,j) has a weight of wi,j, and the street between (i,j) and (i,j+1) has a weight of vi,j.
Simons wants some streets reconstructed. Due to some accidents, some of the streets cannot be reconstructed, while others are optional to be reconstructed.
For each crossroad, if the number of reconstructed streets adjacent to it is even, Simons calls the crossroad elegant. If all the crossroads are elegant, Simons calls the reconstruction nice.
The beauty of a reconstruction is calculated as follows:
- At first, the beauty is 0.
- For each row i from 1 to n−1, let the columns of the streets reconstructed be c1<c2<c3<⋯, add wi,c1−wi,c2+wi,c3−wi,c4+⋯ to the beauty.
- Similarly, for each column j from 1 to m−1, let the rows of the streets reconstructed be r1<r2<r3<⋯, add vr1,j−vr2,j+vr3,j−vr4,j+⋯ to the beauty.
In other words, if a street is an odd-indexed one reconstructed in its row or column, add its weight to the beauty; else subtract it from the beauty.
For example, consider the streets and crossroads below without any streets that cannot be reconstructed:

We can have a nice reconstruction as follows:

The beauty of the reconstruction is 3+4+2−(−9)−(−2)+9+1−(−1)−(−3)−(−4)=38.
Help Simons find the maximum beauty among all the nice reconstructions.
总是不断下落,但我却找不到出口。
——SHUN,《DYING FOR YOU》(链接)
城市中有 n×m 个十字路口,分别命名为 (1,1),(1,2),…,(n,m)。其中第一个数字表示行号,第二个数字表示列号。
仅存在且仅存在两类街道:连接 (i,j) 与 (i+1,j) 的纵向街道,以及连接 (i,j) 与 (i,j+1) 的横向街道。连接 (i,j) 与 (i+1,j) 的纵向街道权重为 wi,j;连接 (i,j) 与 (i,j+1) 的横向街道权重为 vi,j。
西蒙斯希望对部分街道进行重建。但由于某些事故,部分街道不可重建,其余街道则可选择是否重建。
对于每个十字路口,若与其相邻的已重建街道数量为偶数,则西蒙斯称该十字路口为优雅的(elegant);若所有十字路口均为优雅的,则西蒙斯称该重建方案为优美的(nice)。
一次重建方案的美感值(beauty)按如下方式计算:
- 初始时,美感值为 0;
- 对于每一行 i(i=1 到 n−1),设该行中被重建的纵向街道所在列号依次为 c1<c2<c3<⋯,则向美感值累加 wi,c1−wi,c2+wi,c3−wi,c4+⋯;
- 类似地,对于每一列 j(j=1 到 m−1),设该列中被重建的横向街道所在行号依次为 r1<r2<r3<⋯,则向美感值累加 vr1,j−vr2,j+vr3,j−vr4,j+⋯。
换言之:若某条街道在其所在行(纵向街道)或所在列(横向街道)中,是第奇数个被重建的街道,则将其权重加入美感值;否则(即第偶数个)将其权重减去。
例如,考虑下方所示的街道与十字路口(假设不存在不可重建的街道):

一种优美的重建方案如下图所示:

该重建方案的美感值为 3+4+2−(−9)−(−2)+9+1−(−1)−(−3)−(−4)=38。
请帮助西蒙斯求出所有优美重建方案中所能达到的最大美感值。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤5⋅104). The description of the test cases follows.
The first line contains two integers n and m (2≤n,m≤2⋅105; n⋅m≤4⋅105) — the number of rows and columns.
In the next n−1 lines, each line contains m integers wi,j (−109≤wi,j≤109) — the weights of the street between (i,j) and (i+1,j).
In the next n lines, each line contains m−1 integers vi,j (−109≤vi,j≤109) — the weights of the street between (i,j) and (i,j+1).
In the next n−1 lines, each line contains m characters pi,j (pi,j∈0,1) — if pi,j=0, the street between (i,j) and (i+1,j) cannot be reconstructed, and vice versa.
In the next n lines, each line contains m−1 characters qi,j (qi,j∈0,1) — if qi,j=0, the street between (i,j) and (i,j+1) cannot be reconstructed, and vice versa.
It is guaranteed that the sum of n⋅m over all test cases does not exceed 4⋅105.
每个测试包含多个测试用例。第一行包含测试用例数量 t(1≤t≤5⋅104)。随后是各测试用例的描述。
第一行包含两个整数 n 和 m(2≤n,m≤2⋅105;n⋅m≤4⋅105),分别表示行数和列数。
接下来的 n−1 行中,每行包含 m 个整数 wi,j(−109≤wi,j≤109),表示连接 (i,j) 与 (i+1,j) 的街道的权重。
接下来的 n 行中,每行包含 m−1 个整数 vi,j(−109≤vi,j≤109),表示连接 (i,j) 与 (i,j+1) 的街道的权重。
接下来的 n−1 行中,每行包含 m 个字符 pi,j(pi,j∈0,1):若 pi,j=0,则连接 (i,j) 与 (i+1,j) 的街道无法重建;反之亦然。
接下来的 n 行中,每行包含 m−1 个字符 qi,j(qi,j∈0,1):若 qi,j=0,则连接 (i,j) 与 (i,j+1) 的街道无法重建;反之亦然。
保证所有测试用例中 n⋅m 的总和不超过 4⋅105。
输出格式
For each test case, output a single integer — the maximum beauty among all the nice reconstructions.
对于每个测试用例,输出一个整数——所有优美重构中的最大美观度。
输入输出样例
输入#1
4 3 4 2 3 -2 3 4 9 4 -4 3 4 -2 -9 -5 1 6 -1 -3 1111 1111 111 111 111 2 4 4 23 1 35 6 12 -17 -14 1 -40 0100 000 101 3 3 1 0 1 0 1 0 1 0 0 1 0 0 110 111 10 11 11 3 4 13 7 6 -12 3 -5 12 -6 -3 10 -15 -5 8 -11 10 0 -5 1111 0110 110 101 010
输出#1
38 0 4 8
说明/提示
The first test case is explained in the statement.
In the second test case, there is only one nice reconstruction: Simons reconstructs no street. So the maximum beauty is 0.
第一个测试用例已在题目描述中说明。
在第二个测试用例中,仅存在一种优美的重构方案:西蒙斯不重建任何街道。因此,最大美观度为 0。
输入解题思路,AI测评打分。不知道怎么写?