竟然直接暴力广搜过了。
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <map> #include <algorithm> using namespace std; int vstd[205][205][205]; struct bottle { int a,b,c; }; int main() { int T; int a,b,c,d; scanf("%d",&T); while (T--) { struct bottle B; scanf("%d%d%d%d",&a,&b,&c,&d); B.a=B.b=0; B.c=c; memset(vstd,0,sizeof(vstd)); queue<struct bottle> Q; queue<int> water; Q.push(B); water.push(0); vstd[0][0][c]=1; int minn=1000000000; int minnwater; int record=1000000000; int find=0; while (!Q.empty()) { struct bottle front=Q.front(); if ( front.a==d || front.b==d || front.c==d) { if (!find) minnwater=water.front(); else if (water.front()<minnwater) minnwater=water.front(); record=d; find=1; } else if (!find) { if (front.a<d && d-front.a<minn) { minn=d-front.a; record=front.a; minnwater=water.front(); } if (front.b<d && d-front.b<minn) { minn=d-front.b; record=front.b; minnwater=water.front(); } if (front.c<d && d-front.c<minn) { minn=d-front.c; record=front.c; minnwater=water.front(); } } int temp=front.a; int temp2=front.b; front.b+=front.a; front.a=0; if (front.b>b) { front.a=temp-(b-temp2); front.b=b; temp-=front.a; } if (!vstd[front.a][front.b][front.c]) { Q.push(front); water.push(water.front()+temp); vstd[front.a][front.b][front.c]=1; } front=Q.front(); temp=front.a; temp2=front.c; front.c+=front.a; front.a=0; if (front.c>c) { front.a=temp-(c-temp2); front.c=c; temp-=front.a; } if (!vstd[front.a][front.b][front.c]) { Q.push(front); water.push(water.front()+temp); vstd[front.a][front.b][front.c]=1; } front=Q.front(); temp=front.b; temp2=front.a; front.a+=front.b; front.b=0; if (front.a>a) { front.b=temp-(a-temp2); front.a=a; temp-=front.b; } if (!vstd[front.a][front.b][front.c]) { Q.push(front); water.push(water.front()+temp); vstd[front.a][front.b][front.c]=1; } front=Q.front(); temp=front.b; temp2=front.c; front.c+=front.b; front.b=0; if (front.c>c) { front.b=temp-(c-temp2); front.c=c; temp-=front.b; } if (!vstd[front.a][front.b][front.c]) { Q.push(front); water.push(water.front()+temp); vstd[front.a][front.b][front.c]=1; } front=Q.front(); temp=front.c; temp2=front.a; front.a+=front.c; front.c=0; if (front.a>a) { front.c=temp-(a-temp2); front.a=a; temp-=front.c; } if (!vstd[front.a][front.b][front.c]) { Q.push(front); water.push(water.front()+temp); vstd[front.a][front.b][front.c]=1; } front=Q.front(); temp=front.c; temp2=front.b; front.b+=front.c; front.c=0; if (front.b>b) { front.c=temp-(b-temp2); front.b=b; temp-=front.c; } if (!vstd[front.a][front.b][front.c]) { Q.push(front); water.push(water.front()+temp); vstd[front.a][front.b][front.c]=1; } Q.pop(); water.pop(); } printf("%d %d\n",minnwater,record); } }