There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The rst and the second jug
are initially empty, while the third is completely lled with water. It is allowed to pour water from one jug into another until either the rst one
is empty or the second one is full. This operation can be performed zero, one or more times.
You are to write a program that computes the least total amount of water that needs to be poured; so that at least one of the jugs contains
exactly d liters of water (d is a positive integer not greater than 200). If it is not possible to measure d liters this way your program should
nd a smaller amount of water d′ < d which is closest to d and for which d′ liters could be produced. When d′ is found, your program should compute the least total amount of poured water needed to produce d′ liters in at least one of the jugs.
Input
The rst line of input contains the number of test cases. In the next T lines, T test cases follow. Each test case is given in one line of input
containing four space separated integers | a, b, c and d.
Output
The output consists of two integers separated by a single space. The rst integer equals the least total amount (the sum of all waters you
pour from one jug to another) of poured water. The second integer equals d, if d liters of water could be produced by such transformations,
or equals the closest smaller value d′ that your program has found.
Sample Input
2
2 3 4 2
96 97 199 62
Sample Output
2 2
9859 62
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<cmath> #include<algorithm> #include <map> #include <queue> using namespace std; //#define lint long long int bool v[210][210][210]; struct node { int z[3],bu; friend bool operator < (node a,node b) { return a.bu>b.bu; } }; int dfs(int *,int); int main() { int ci; scanf("%d",&ci); while(ci--) { memset(v,0,sizeof(v)); int z[3],m; scanf("%d%d%d%d",&z[0],&z[1],&z[2],&m); dfs(z,m); } return 0; } int dfs(int *z,int m) { priority_queue <node>qu; node a,ans,b; ans.z[0]=0,ans.bu=0; a.z[0]=0,a.z[1]=0,a.z[2]=z[2],a.bu=0; qu.push(a); while(!qu.empty()) { //cout<<'!'; a=qu.top(); qu.pop(); if(v[a.z[0]][a.z[1]][a.z[2]]) continue; v[a.z[0]][a.z[1]][a.z[2]]=1; if(a.z[0]==m||a.z[1]==m||a.z[2]==m) { //cout<<a.z[0]<<'\t'<<a.z[1]<<'\t'<<a.z[2]<<'\t'<<m<<endl; ans=a; ans.z[0]=m; break; } for(int i=0; i<3; i++) if(a.z[i]>ans.z[0]&&a.z[i]<m) { //cout<<'@'; ans.z[0]=a.z[i]; ans.bu=a.bu; } for(int i=0; i<3; i++) for(int j=0; j<3; j++) if(i!=j) if(a.z[i]&&a.z[j]<z[j]) { int t=min(z[j]-a.z[j],a.z[i]); b.z[j]=a.z[j]+t; b.z[i]=a.z[i]-t; b.z[3-i-j]=a.z[3-i-j]; b.bu=a.bu+t; qu.push(b); } } printf("%d %d\n",ans.bu,ans.z[0]); }