#include <bits/stdc++.h> using namespace std; int main(){ //int q; cin >> q; for(q; q > 0; q --){ int n, n1, n2, n3; n=0; cin >> n1; cin >> n2; cin >> n3; int b1,b2,b3; cin >>b1>>b2>>b3; priority_queue<pair<long long, tuple<int ,int ,int>>> q; //koszt, stan map<tuple<int ,int ,int>, long long> m; //stan->minimalny koszt dojścia do tego stanu q.push(make_pair(-1,make_tuple(b1, b2, b3))); // m[make_tuple(n1, 0, 0)] = 0; vector<int> ns; //pojemności ns.push_back(n1); ns.push_back(n2); ns.push_back(n3); long long sum = LLONG_MAX; vector<long long> res(n3+1,LLONG_MAX); while(!q.empty()){ tuple<int, int, int> stan = q.top().second; long long dystans = - q.top().first; q.pop(); // if((get<0>(stan) == n or get<1>(stan) == n or get<2>(stan) == n) and dystans -1 < sum) // sum = dystans-1; res[get<0>(stan)] = min(res[get<0>(stan)],dystans-1); res[get<1>(stan)] = min(res[get<1>(stan)],dystans-1); res[get<2>(stan)] = min(res[get<2>(stan)],dystans-1); if(m[stan] == 0 or m[stan] >= dystans){ m[stan] = dystans; vector<int> is(3); tie(is[0], is[1], is[2]) = stan; auto go = [&](int a, int b){ vector<int> is2 = is; int ch = min(ns[a] - is[a], is[b]); is2[a] += ch; is2[b] -= ch; tuple<int, int, int> next = make_tuple(is2[0], is2[1], is2[2]); if(m[next] == 0 or m[next] > 1 + dystans){ m[next] = 1+ dystans; q.push(make_pair(-1-dystans, next)); } }; go(0,1); go(0,2); go(1,0); go(1,2); go(2,0); go(2,1); } } /* if(sum == LLONG_MAX) cout << "NIE" << endl; else cout << sum << endl;*/ for(auto x:res)cout<<((x==LLONG_MAX)?-1:x)<<" "; /* int ch1 = min(n1 - i, j); tuple<int, int, int> next1 = make_tuple(i + ch1, j - ch1, k); if(m[next1] == 0 or m[next1] > ch + dystans){ m[next1] = ch + dystans; q.push(next1); } */ }//}
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <bits/stdc++.h> using namespace std; int main(){ //int q; cin >> q; for(q; q > 0; q --){ int n, n1, n2, n3; n=0; cin >> n1; cin >> n2; cin >> n3; int b1,b2,b3; cin >>b1>>b2>>b3; priority_queue<pair<long long, tuple<int ,int ,int>>> q; //koszt, stan map<tuple<int ,int ,int>, long long> m; //stan->minimalny koszt dojścia do tego stanu q.push(make_pair(-1,make_tuple(b1, b2, b3))); // m[make_tuple(n1, 0, 0)] = 0; vector<int> ns; //pojemności ns.push_back(n1); ns.push_back(n2); ns.push_back(n3); long long sum = LLONG_MAX; vector<long long> res(n3+1,LLONG_MAX); while(!q.empty()){ tuple<int, int, int> stan = q.top().second; long long dystans = - q.top().first; q.pop(); // if((get<0>(stan) == n or get<1>(stan) == n or get<2>(stan) == n) and dystans -1 < sum) // sum = dystans-1; res[get<0>(stan)] = min(res[get<0>(stan)],dystans-1); res[get<1>(stan)] = min(res[get<1>(stan)],dystans-1); res[get<2>(stan)] = min(res[get<2>(stan)],dystans-1); if(m[stan] == 0 or m[stan] >= dystans){ m[stan] = dystans; vector<int> is(3); tie(is[0], is[1], is[2]) = stan; auto go = [&](int a, int b){ vector<int> is2 = is; int ch = min(ns[a] - is[a], is[b]); is2[a] += ch; is2[b] -= ch; tuple<int, int, int> next = make_tuple(is2[0], is2[1], is2[2]); if(m[next] == 0 or m[next] > 1 + dystans){ m[next] = 1+ dystans; q.push(make_pair(-1-dystans, next)); } }; go(0,1); go(0,2); go(1,0); go(1,2); go(2,0); go(2,1); } } /* if(sum == LLONG_MAX) cout << "NIE" << endl; else cout << sum << endl;*/ for(auto x:res)cout<<((x==LLONG_MAX)?-1:x)<<" "; /* int ch1 = min(n1 - i, j); tuple<int, int, int> next1 = make_tuple(i + ch1, j - ch1, k); if(m[next1] == 0 or m[next1] > ch + dystans){ m[next1] = ch + dystans; q.push(next1); } */ }//} |