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 first and the second jug are initially empty, while the third
is completely filled with water. It is allowed to pour water from one jug into another until either the first 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 find 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 first 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 first 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 | Sample Output |
2 2 3 4 2 96 97 199 62 | 2 2 9859 62 |
题意:
只有第三个杯子里面有装满水,第一和第二个杯子里面都是空,那么这个状态可以转移的话,只能有两个方向的转移。第一是向第一杯子倒水,第二是向第二个杯子倒水,那么这道题,对于一个状态,最多有6种转移的方向,最少是没有可以转移的现在给出目标水量,要求输出可以的到目标水量的移动水量最小值,如果不能到达,输出最接近的。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 205;
const int INF = 0x7f7f7f7f;
const int dir[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
struct state {
int cup[3];
int step;
state (int a = 0, int b = 0, int c = 0, int step = 0) {
this->cup[0] = a;
this->cup[1] = b;
this->cup[2] = c;
this->step = step;
}
};
int Cup[3], D, d[maxn][maxn];
bool add_state (state u) {
if (d[u.cup[0]][u.cup[1]] > u.step) {
d[u.cup[0]][u.cup[1]] = u.step;
return true;
}
return false;
}
void BFS () {
state s(0, 0, Cup[2], 0);
queue<state> que;
que.push(s);
while (!que.empty()) {
state u = que.front();
que.pop();
if (!add_state(u))
continue;
for (int i = 0; i < 6; i++) {
state k;
k.cup[dir[i][0]] = min(u.cup[dir[i][0]] + u.cup[dir[i][1]], Cup[dir[i][0]]);
k.cup[dir[i][1]] = u.cup[dir[i][1]] - k.cup[dir[i][0]] + u.cup[dir[i][0]];
k.cup[dir[i][2]] = u.cup[dir[i][2]];
k.step = u.step + k.cup[dir[i][0]] - u.cup[dir[i][0]];
que.push(k);
}
}
}
bool search (int x) {
int ans = INF;
for (int i = 0; i <= Cup[2] - x; i++) {
ans = min(ans, d[x][i]);
ans = min(ans, d[i][x]);
ans = min(ans, d[i][Cup[2] - x - i]);
}
if (ans == INF)
return false;
printf("%d %d\n", ans, x);
return true;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d%d%d", &Cup[0], &Cup[1], &Cup[2], &D);
memset(d, INF, sizeof(d));
BFS();
for (int i = D; i >= 0; i--) {
if (search(i))
break;
}
}
return 0;
}