#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 1e5; struct Event{ int a; int b; int c; int cnt; }; int A,B,C,rab,rba,rac,rca,rbc,rcb; int AA[MAXN+10], BB[MAXN+10], CC[MAXN+10]; int reszta(int E, int k, int l); void przelej(int a,int b,int c,int cnt); queue<Event> kolejka; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a,b,c,m; cin>>A>>B>>C>>a>>b>>c; for(int i=0;i<=MAXN;++i){ AA[i]=INT_MAX; BB[i]=INT_MAX; CC[i]=INT_MAX; } kolejka.push({a,b,c,0}); while(!kolejka.empty()){ auto [a,b,c,cnt] = kolejka.front(); kolejka.pop(); przelej(a,b,c,cnt); } for(int i=0;i<=C;++i){ m=min(min(AA[i],BB[i]),CC[i]); cout<< ( m==INT_MAX ? -1 : m) <<" "; } return 0; } void przelej(int a,int b,int c,int cnt){ AA[a]=min(AA[a],cnt); BB[b]=min(BB[b],cnt); CC[c]=min(CC[c],cnt); rab=reszta(B,a,b); rba=reszta(A,a,b); rac=reszta(C,a,c); rca=reszta(A,a,c); rbc=reszta(C,b,c); rcb=reszta(B,b,c); // cout<<rab<<" "<<rba<<" "<<rac<<" "<<rca<<" "<<rbc<<" "<<rcb<<"\n"; if(BB[a+b-rab]==INT_MAX || AA[rab]==INT_MAX) kolejka.push({rab,a+b-rab,c,cnt+1}); if(AA[a+b-rba]==INT_MAX || BB[rba]==INT_MAX) kolejka.push({a+b-rba,rba,c,cnt+1}); if(CC[a+c-rac]==INT_MAX || AA[rac]==INT_MAX) kolejka.push({rac,b,a+c-rac,cnt+1}); if(AA[a+c-rca]==INT_MAX || CC[rca]==INT_MAX) kolejka.push({a+c-rca,b,rca,cnt+1}); if(BB[b+c-rcb]==INT_MAX || CC[rcb]==INT_MAX) kolejka.push({a,b+c-rcb,rcb,cnt+1}); if(CC[b+c-rbc]==INT_MAX || BB[rbc]==INT_MAX) kolejka.push({a,rbc,b+c-rbc,cnt+1}); } int reszta(int E, int k, int l){ if(k+l-E<0) return 0; else return k+l-E; }
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 | #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 1e5; struct Event{ int a; int b; int c; int cnt; }; int A,B,C,rab,rba,rac,rca,rbc,rcb; int AA[MAXN+10], BB[MAXN+10], CC[MAXN+10]; int reszta(int E, int k, int l); void przelej(int a,int b,int c,int cnt); queue<Event> kolejka; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a,b,c,m; cin>>A>>B>>C>>a>>b>>c; for(int i=0;i<=MAXN;++i){ AA[i]=INT_MAX; BB[i]=INT_MAX; CC[i]=INT_MAX; } kolejka.push({a,b,c,0}); while(!kolejka.empty()){ auto [a,b,c,cnt] = kolejka.front(); kolejka.pop(); przelej(a,b,c,cnt); } for(int i=0;i<=C;++i){ m=min(min(AA[i],BB[i]),CC[i]); cout<< ( m==INT_MAX ? -1 : m) <<" "; } return 0; } void przelej(int a,int b,int c,int cnt){ AA[a]=min(AA[a],cnt); BB[b]=min(BB[b],cnt); CC[c]=min(CC[c],cnt); rab=reszta(B,a,b); rba=reszta(A,a,b); rac=reszta(C,a,c); rca=reszta(A,a,c); rbc=reszta(C,b,c); rcb=reszta(B,b,c); // cout<<rab<<" "<<rba<<" "<<rac<<" "<<rca<<" "<<rbc<<" "<<rcb<<"\n"; if(BB[a+b-rab]==INT_MAX || AA[rab]==INT_MAX) kolejka.push({rab,a+b-rab,c,cnt+1}); if(AA[a+b-rba]==INT_MAX || BB[rba]==INT_MAX) kolejka.push({a+b-rba,rba,c,cnt+1}); if(CC[a+c-rac]==INT_MAX || AA[rac]==INT_MAX) kolejka.push({rac,b,a+c-rac,cnt+1}); if(AA[a+c-rca]==INT_MAX || CC[rca]==INT_MAX) kolejka.push({a+c-rca,b,rca,cnt+1}); if(BB[b+c-rcb]==INT_MAX || CC[rcb]==INT_MAX) kolejka.push({a,b+c-rcb,rcb,cnt+1}); if(CC[b+c-rbc]==INT_MAX || BB[rbc]==INT_MAX) kolejka.push({a,rbc,b+c-rbc,cnt+1}); } int reszta(int E, int k, int l){ if(k+l-E<0) return 0; else return k+l-E; } |