There is three jugs with a volume of a, B and C liters. (A, B, and C is positive integers not greater
than 200). The. RST and the second jug are initially empty, while the third was 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 is performed zero, one or more times.
You-to-write a program, that computes the least total amount of water, needs to be poured;
So, at least one of the jugs contains exactly d liters of water (d was a positive integer not greater
than 200). If It isn't possible to measure D liters the this by 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 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 was given in one line of the input containing four space separated integers-a, B, C and D.
Output
The output consists of integers separated by a single space. The. Rst integer equals the least total
Amount (the sum of all waters your 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
Test instructions: Tell three cups, the total capacity is a,b,c, originally only C full, the others are empty, the cup has no scale, can only pour water, ask whether the final can make a cup has d units of water, if not reach D, then judge can reach the first unit less than D can reach, the output of the smallest amount of water poured.
Thinking: Think of the state as a node, each time the expansion of the minimum amount of water to remove the nodes, and then various transfer.
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include < algorithm> #include <cmath> #include <queue>using namespace Std;int cap[3];int vis[250][250];int ans[250 ];struct node{int V[3],dis; BOOL operator < (const node& RHS) Const {return rhs.dis<dis; }}f,t;void Check (const node& u) {for (int i=0;i<3;i++) {int a=u.v[i]; if (ans[a]<0 | | u.dis<ans[a]) Ans[a]=u.dis; }}void Solve (int a,int b,int c,int d) {cap[0]=a;cap[1]=b;cap[2]=c; Memset (ans,-1,sizeof ans); memset (vis,0,sizeof Vis); priority_queue<node>pq; Vis[0][0]=1; f.dis=0; f.v[0]=0; f.v[1]=0; F.v[2]=c; Pq.push (f); while (!pq.empty ()) {f=pq.top (); Pq.pop (); Check (f); if (ans[d]>=0) break; for (int i=0;i<3;i++) for (int j=0;j<3;j++) {if (i==j) continue; if (f.v[i]==0 | | f.v[j]==cap[j]) continue; int tmp=min (CAP[J],F.V[I]+F.V[J])-f.v[j]; t=f;//only two of the three parts of the operation, the third part of the direct assignment of the past t.dis=f.dis+tmp; t.v[i]=f.v[i]-tmp; t.v[j]=f.v[j]+tmp; if (!vis[t.v[0]][t.v[1]]) {vis[t.v[0]][t.v[1]]=1; Pq.push (t); }}} while (d>=0) {if (ans[d]>=0) { printf ("%d%d\n", ans[d],d); Return } d--; }}int Main () {int a,b,c,d; int T; scanf ("%d", &t); {while (t--) {scanf ("%d%d%d%d", &a,&b,&c,&d); Solve (A,B,C,D); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10603 fill pour water problem