#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define REPD(i,n) FORD(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define PB push_back
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) ((int)(c).size())
#define MP make_pair
#define FT first
#define SD second
#define CLEAR(a) memset((a), 0, sizeof(a))
#define INT(x) int x; scanf("%d", &x)
#define LLG(x) LL x; scanf("%lld", &x)
#define STR(n,x) char x[n]; scanf("%s", x)
#define LINE(n,x) char x[n]; fgets(x, n, stdin)
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<PII> VII;
typedef vector<double> VD;
typedef vector<LD> VLD;
typedef vector<LL> VLL;
typedef vector<bool> VB;
typedef pair<PII, int> T;
const int INF = 2000000000;
int ma[3];
int res[100001];
T tri(int a, int b, int c) {
return MP(MP(a, b), c);
}
void update(const T& t, int x) {
res[t.FT.FT] = min(res[t.FT.FT], x);
res[t.FT.SD] = min(res[t.FT.SD], x);
res[t.SD] = min(res[t.SD], x);
}
vector<T> next(const T& t) {
VI v;
v.PB(t.FT.FT);
v.PB(t.FT.SD);
v.PB(t.SD);
vector<T> r;
REP(i,3) if (v[i])
REP(j,3) if (i != j && v[j] < ma[j]) {
int x = min(v[i], ma[j] - v[j]);
VI w = v;
w[i] -= x;
w[j] += x;
r.PB(tri(w[0], w[1], w[2]));
}
return r;
}
int main() {
INT(A);
INT(B);
INT(C);
ma[0] = A;
ma[1] = B;
ma[2] = C;
INT(a);
INT(b);
INT(c);
REP(i,C+1) res[i] = INF;
map<T, int> d;
T s = tri(a, b, c);
d[s] = 0;
update(s, 0);
queue<T> q;
q.push(s);
while (!q.empty()) {
T v = q.front();
q.pop();
vector<T> w = next(v);
FORE(it,w) {
if (d.find(*it) != d.end()) continue;
update(*it, d[*it] = d[v] + 1);
q.push(*it);
}
}
REP(i,C+1) {
if (i) printf(" ");
printf("%d", res[i] < INF ? res[i] : -1);
}
printf("\n");
}
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define SIZE(c) ((int)(c).size()) #define MP make_pair #define FT first #define SD second #define CLEAR(a) memset((a), 0, sizeof(a)) #define INT(x) int x; scanf("%d", &x) #define LLG(x) LL x; scanf("%lld", &x) #define STR(n,x) char x[n]; scanf("%s", x) #define LINE(n,x) char x[n]; fgets(x, n, stdin) typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<string> VS; typedef vector<VI> VVI; typedef vector<PII> VII; typedef vector<double> VD; typedef vector<LD> VLD; typedef vector<LL> VLL; typedef vector<bool> VB; typedef pair<PII, int> T; const int INF = 2000000000; int ma[3]; int res[100001]; T tri(int a, int b, int c) { return MP(MP(a, b), c); } void update(const T& t, int x) { res[t.FT.FT] = min(res[t.FT.FT], x); res[t.FT.SD] = min(res[t.FT.SD], x); res[t.SD] = min(res[t.SD], x); } vector<T> next(const T& t) { VI v; v.PB(t.FT.FT); v.PB(t.FT.SD); v.PB(t.SD); vector<T> r; REP(i,3) if (v[i]) REP(j,3) if (i != j && v[j] < ma[j]) { int x = min(v[i], ma[j] - v[j]); VI w = v; w[i] -= x; w[j] += x; r.PB(tri(w[0], w[1], w[2])); } return r; } int main() { INT(A); INT(B); INT(C); ma[0] = A; ma[1] = B; ma[2] = C; INT(a); INT(b); INT(c); REP(i,C+1) res[i] = INF; map<T, int> d; T s = tri(a, b, c); d[s] = 0; update(s, 0); queue<T> q; q.push(s); while (!q.empty()) { T v = q.front(); q.pop(); vector<T> w = next(v); FORE(it,w) { if (d.find(*it) != d.end()) continue; update(*it, d[*it] = d[v] + 1); q.push(*it); } } REP(i,C+1) { if (i) printf(" "); printf("%d", res[i] < INF ? res[i] : -1); } printf("\n"); } |
English