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
| #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <cctype> #include <queue> #define ll long long #define ri register int #define ull unsigned long long using std::priority_queue; using std::min; using std::swap; template <class T>inline void read(T &x){ x=0;int ne=0;char c; while(!isdigit(c=getchar()))ne=c=='-'; x=c-48; while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48; x=ne?-x:x;return ; } const int maxn=205; const int inf=0x7fffffff; int ans=inf,step=0; int a,b,c,d; struct Sta{ int bot[3]; int sum; Sta(){bot[0]=bot[1]=bot[2]=sum=0;} Sta(int _x,int _y,int _z,int _sum){bot[0]=_x,bot[1]=_y,bot[2]=_z,sum=_sum;} inline bool update(){ for(ri i=0;i<3;i++){ if(bot[i]>d)continue; if(ans>d-bot[i]){ ans=d-bot[i]; step=sum; } } if(!ans)return 1; return 0; } bool operator <(const Sta &b)const{ return sum>b.sum; } }Tmp; bool vis[205][205]; int size[3],now[3]; int t; inline void bfs(){ memset(vis,0,sizeof(vis)); ans=inf,step=0; priority_queue <Sta> q; while(q.size())q.pop(); q.push(Sta(0,0,c,0)); vis[0][0]=1; int x,y,z,lef,o,p; while(q.size()){ Tmp=q.top();q.pop(); if(Tmp.update()){ printf("%d %d\n",Tmp.sum,d-ans); return ; } now[0]=Tmp.bot[0],now[1]=Tmp.bot[1],now[2]=Tmp.bot[2],o=Tmp.sum; for(ri i=0;i<3;i++){ for(ri j=0;j<3;j++){ if(!now[i]||size[j]==now[j]||i==j)continue; lef=size[j]-now[j]; if(now[i]>=lef){ now[i]-=lef; now[j]=size[j]; if(!vis[now[0]][now[1]]){ vis[now[0]][now[1]]=1; q.push(Sta(now[0],now[1],now[2],o+lef)); } now[i]+=lef; now[j]-=lef; } else{ p=now[i]; now[i]=0; now[j]+=p; if(!vis[now[0]][now[1]]){ vis[now[0]][now[1]]=1; q.push(Sta(now[0],now[1],now[2],o+p)); } now[i]=p; now[j]-=p; } } } } printf("%d %d\n",step,d-ans); return ; } int main(){ read(t); while(t--){ read(a),read(b),read(c),read(d); size[0]=a,size[1]=b,size[2]=c; bfs(); } return 0; }
|