include bits stdc using namespace std int len string class fastio publ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
int t, n, a, b, len;
string s;
class fastio {
public:
fastio() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
}
} __fastio;
int f(int p, int i) {
//Base Case
if(p == 1 && i == len-1) return (a+2*b);
else if(p == 2 && i == len-1) return ((2*a)+2*b);
/*
when there is crossroad (i.e 1) then x should be h = 2 and x + 1 could be h = 1 or 2 it depends
on the value infront, for no crossroad (i.e 0) 'x' height could be either 1 or 2 depends on the
optimal answer
*/
if(s[i] == '0' && p == 1) {
/*
if the infront value is 0 and past value of pillar (here p in f(p)) is 1, then there are two
cases either front pillar could be h = 1 or 2, if 1 then cost would be cost of
pillar + gasline that is --> a + b
if h = 2 then cost would be 2 * unit pillar + 2 * unit gasline
that is --> 2(a+b)
*/
return min(f(2,i+1)+2*(a+b), f(1,i+1)+a+b);
}
else if(s[i] == '1' && p == 1) {
/*
if infront value is 1, and past pillar is h = 1 then cost is 2 * pillar cost + gasline
*/
return f(1,i+1)+(2*b)+a;
}
else if(s[i] == '0' && p == 2) {
/*
if the past pillar height is 2 and the infront value is 0, then h = 1 or 2, if 1 then cost
would be 2a + b else 2b + a
*/
return min(f(1,i+1)+(2*a)+b, f(2,i+1)+(2*b)+a);
}
else if(s[i] == 1 && p == 2) {
/*
if the past pillar height is 2 and the infront value is 1 then h will be 2 and cost will be
2b + a
*/
return f(2,i+1)+(2*b)+a;
}
}
void solve() {
cin >> n >> a >> b;
cin >> s;
len = s.length();
cout << f(1,1) << endl;
}
int main() {
__fastio;
cin >> t;
while(t--) {
solve();
}
return 0;
}