#include using namespace std; #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() #define spa << " " << #define test cout<<"test"<= (ll)(m); i--) typedef long long ll; typedef long double ld; const ll MOD = 1e9+7; //const ll MOD = 998244353; const ll INF = 1e18; using P = pair; template void chmin(T &a,T b){if(a>b)a=b;} template void chmax(T &a,T b){if(a void ans(bool x,T1 y,T2 z){if(x)cout<>v,ll h,ll w){for(ll i=0;iv,ll h,ll w){for(ll i=0;iv,ll n){cout<>vec(ll x, ll y, ll w){ vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while((r=x%y)!=0){x=y;y=r;}return y;} //m.emplace(x,0).fi->se++; ll MAX_V = 1e5 + 5; //頂点数 struct edge { ll to; ll cost; }; //<最短距離, 頂点の番号>using P = pair; vector> G(MAX_V); //隣接リスト vector d(MAX_V); //各頂点への最短距離 void dijkstra(ll s) { priority_queue, greater

> que; fill(ALL(d), INF); d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (ll i=0; i d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(false); ll res=0,res1=INF,res2=-INF,buf=0; bool judge = true; ll x[3];cin>>x[0]>>x[1]>>x[2]; buf=x[0]+x[1]+x[2]; char p,q; ll t0,t1;cin>>p>>t0>>q>>t1; ll s0=p-'A',s1=q-'A'; res=INF; for(ll i=0;i<2;i++){ rep(j,i+1,3){ struct edge e ={j*2,1}; G[i*2].push_back(e); e ={i*2,1}; G[j*2].push_back(e); e ={j*2+1,1}; G[i*2+1].push_back(e); e ={i*2+1,1}; G[j*2+1].push_back(e); } struct edge e ={i*2,x[i]-1}; G[i*2+1].push_back(e); e ={i*2+1,x[i]-1}; G[i*2].push_back(e); } rep(i,0,3){ if(s0==i){ struct edge e ={i*2,abs(t0-1)}; G[6].push_back(e); e ={6,abs(t0-1)}; G[i*2].push_back(e); e ={i*2+1,abs(x[i]-t0)}; G[6].push_back(e); e ={6,abs(x[i]-t0)}; G[i*2+1].push_back(e); } if(s1==i){ struct edge e ={i*2,abs(t1-1)}; G[7].push_back(e); e ={7,abs(t1-1)}; G[i*2].push_back(e); e ={i*2+1,abs(x[i]-t1)}; G[7].push_back(e); e ={7,abs(x[i]-t1)}; G[i*2+1].push_back(e); } } dijkstra(6); cout<