CF1443B.Saving the City
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Bertown is a city with n buildings in a straight line.
The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length n, where the i-th character is "1" if there is a mine under the building number i and "0" otherwise.
Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered x is activated, it explodes and activates two adjacent mines under the buildings numbered x−1 and x+1 (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes a coins. He can repeat this operation as many times as you want.
Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes b coins. He can also repeat this operation as many times as you want.
The sapper can carry out operations in any order.
You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.
伯特城是一座拥有 n 座建筑的城市,这些建筑排成一条直线。
城市安全部门发现其中一些建筑下方埋有地雷。他们绘制了一张地图,该地图是一个长度为 n 的字符串,其中第 i 个字符为 "1" 表示第 i 号建筑下方埋有地雷,为 "0" 则表示没有地雷。
伯特城最优秀的排雷专家掌握一种特殊的引爆技术,可确保上方的建筑不会因爆炸而受损。当引爆位于第 x 号建筑下方的地雷时,该地雷会爆炸,并进一步触发其左右相邻的两处地雷(即分别位于第 x−1 号和第 x+1 号建筑下方的地雷);若某相邻位置原本没有地雷,则该位置无任何反应。因此,在一段连续的地雷区间中,只需手动引爆其中任意一颗地雷,即可引发连锁反应,引爆整段区间内的所有地雷。手动引爆一颗地雷需花费 a 枚金币,该操作可执行任意多次。
此外,排雷专家还可以在原本没有地雷的建筑下方增设一颗地雷,每次执行此操作需花费 b 枚金币,该操作也可执行任意多次。
排雷专家可以按任意顺序执行上述操作。
你的目标是彻底清除城市中所有地雷,使城市恢复安全。请计算排雷专家为达成此目标所需支付的最少金币数量。
输入格式
The first line contains one positive integer t (1≤t≤105) — the number of test cases. Then t test cases follow.
Each test case begins with a line containing two integers a and b (1≤a,b≤1000) — the cost of activating and placing one mine, respectively.
The next line contains a map of mines in the city — a string consisting of zeros and ones.
The sum of the string lengths for all test cases does not exceed 105.
第一行包含一个正整数 t(1≤t≤105)—— 表示测试用例的数量。随后是 t 个测试用例。
每个测试用例以一行开始,该行包含两个整数 a 和 b(1≤a,b≤1000)—— 分别表示激活一枚地雷和放置一枚地雷的代价。
接下来一行包含城市中地雷的地图——一个由 0 和 1 组成的字符串。
所有测试用例中字符串长度的总和不超过 105。
输出格式
For each test case, output one integer — the minimum number of coins that the sapper will have to pay.
对于每个测试用例,输出一个整数——工兵需要支付的最少硬币数量。
输入输出样例
输入#1
2 1 1 01000010 5 1 01101110
输出#1
2 6
说明/提示
In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, b=1 coin for placing a mine and a=5 coins for activating.
在第二个测试用例中,若我们在第四栋建筑下方放置一枚地雷,然后激活它,则场地上的所有地雷都会被激活。该操作的花费为六枚硬币:放置地雷花费 b=1 枚硬币,激活花费 a=5 枚硬币。
输入解题思路,AI测评打分。不知道怎么写?