#include #include #include #include using namespace std; using ll = long long; using vi = vector; void ins() {} templatevoid ins(T& v,Rest&... rest){cin>>v;ins(rest...);} #define rep(i,n) for(int i=0,_i=(n);i<_i;++i) struct Graph { vector> edge; Graph(int n = 0) { resize(n); } void resize(int n) { edge.resize(n, map()); } void addEdge(int from, int to, long long weight) { edge[from][to] = weight; edge[to][from] = weight; } map& operator[](int from) { return edge[from]; } }; templateusing priority_queue_rev = priority_queue, greater>; int main() { ll XYZ[3]; cin >> XYZ[0] >> XYZ[1] >> XYZ[2]; char s0, s1; ll t0, t1; cin >> s0 >> t0 >> s1 >> t1; if (s0 > s1) { swap(s0, s1); swap(t0, t1); } if (t0 > t1) swap(t0, t1); Graph G(8); // A1, Ax, B1, By, C1, Cz, start, goal の8つ G.addEdge(0, 2, 1); G.addEdge(0, 4, 1); G.addEdge(2, 4, 1); G.addEdge(1, 3, 1); G.addEdge(1, 5, 1); G.addEdge(3, 5, 1); if (s0 == s1) { int a = s0-'A', b = (s0-'A'+1)%3, c = (s0-'A'+2)%3; G.addEdge(2*b, 2*b+1, XYZ[b]-1); G.addEdge(2*c, 2*c+1, XYZ[c]-1); G.addEdge(2*a, 6, t0-1); G.addEdge(6, 7, t1-t0); G.addEdge(7, 2*a+1, XYZ[a]-t1); } else { int b = s0-'A', c = s1-'A'; int a = 6 / (b+1) / (c+1) - 1; G.addEdge(2*a, 2*a+1, XYZ[a]-1); G.addEdge(2*b, 6, t0-1); G.addEdge(6, 2*b+1, XYZ[b]-t0); G.addEdge(2*c, 7, t1-1); G.addEdge(7, 2*c+1, XYZ[c]-t1); } priority_queue_rev> q; q.emplace(0, 6); vector distance(8, 1e16); distance[6] = 0; while (!q.empty()) { auto [c, from] = q.top(); q.pop(); if (from == 7) { cout << distance[from] << endl; break; } for (const auto& [to, cost]:G[from]) { if (distance[to] <= cost + distance[from]) continue; q.emplace(distance[from] + cost, to); distance[to] = cost + distance[from]; } } return 0; }