https://codeforces.com/contest/1543/problem/C
Simple depth first search is enough.
First, explain the meaning of the question. Here are four parameters c, m, P and V. start to execute. You choose a legal path in C, m, P to execute. There are some rules, it’s like this.
1 If this parameter is greater than a minimum (I take 1E-9), it is legal. We can only choose the legal path.
2 If we choose P, then this path ends. The total weight of this path is the number of nodes multiplied by the weight of each node, such as C1 * P2 * C3 * P4 * 4. Here, 1234 is to distinguish, but does not mean that C3 is equal every time. The calculation method of C3 is as follows
3 If C, m and P are all valid now, we can choose the path of C. If we take the road of C, we need to update C, m, P. C needs to subtract a number. If C is larger than V, then this number will take v. otherwise, this number will take C (it’s easy to understand that C can’t be negative), and the subtracted number will be recorded as D. what can we do? We know that P must be valid. If m is valid, then M and P divide d equally, that is, add D / 2 to each. If M is invalid, add d to P, and then use the new C, m, P just keep going
4 In addition, we need parameters to record how much we have gone, so we can add parameters to CNT. We also need to record the weight of the current path, so we can add the parameter U
Code:
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
int t;
ld c, m, p, v, d, res;
void dfs(ld c, ld m, ld p, ld v, ld u, ld cnt)
{
res += u * p * cnt;
// cout << c << ' ' << m << ' ' << p << ' ' << u * p << ' ' << cnt << ' ' << res << endl;
if(c > 1e-9)
{
d = min(c / 2, v / 2);
if(m > 1e-9) dfs(c - d * 2, m + d, p + d, v, u * c, cnt + 1);
else dfs(c - d * 2, m, p + d * 2, v, u * c, cnt + 1);
}
if(m > 1e-9)
{
d = min(m / 2, v / 2);
if(c > 1e-9) dfs(c + d, m - 2 * d, p + d, v, u * m, cnt + 1);
else dfs(c, m - 2 * d, p + 2 * d, v, u * m, cnt + 1);
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
cout << fixed << setprecision(12);
while(t --)
{
res = 0;
cin >> c >> m >> p >> v;
dfs(c, m, p, v, 1, 1);
cout << res << endl;
}
}