题目点此跳转
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. Eachtest 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
2
2 3 4 2
96 97 199 62
Sample Output
2 2
9859 62
思路
题目意思是设3个杯子的容量分别为a, b, c,最初只有第3个杯子装满了c升水,其他两个杯子为空。最少需要倒多少升水才能让某一个杯子中的水有d升呢?如果无法做到恰好d升,就让某一个杯子里的水是d ’ 升,其中d ’ < d并且尽量接近d。(1≤a,b,c,d≤200)。要求输出最少的倒水量和目标水量(d或者d’)。
一道很裸的状态空间搜索题,直接bfs即可,注意以下几点:
- 优先选择水量少的出队.
- 一个状态不是由个杯子的水量决定的, 而是由两个杯子的水量决定的,只用一个201*201的二维数组即可.
代码
#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a,b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
#define OT freopen("out.txt", "w", stdout);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 2e2 + 100;
const int INF = 0x7fffffff;
const int dir[4][2] = {-1,0,1,0,0,-1,0,1};
int a[4], d, dp, ansp;
int vis[maxn][maxn];
void init() { met(vis, 0); dp = 0; ansp = INF; }
struct node {
int x[4], ans;
node(){}
node(int i, int j, int k, int l):ans(l){ x[1] = i, x[2] = j, x[3] = k; }
bool canP(int i, int j) {
if(x[i] == 0 || x[j] == a[j]) return 0;
return 1;
}
int pour(int i, int j) {
int res = min(x[i], a[j]-x[j]);
x[i] -= res; x[j] += res;
ans += res; return res;
}
bool check() {
if(vis[x[1]][x[2]]) return 1;
vis[x[1]][x[2]] = 1; return 0;
}
bool reach() {
for(int i = 1; i <= 3; ++i) {
if(x[i] < d && x[i] > dp) dp = x[i], ansp = ans;
else if(x[i] < d && x[i] == dp) ansp = min(ansp, ans);
}
for(int i = 1; i <= 3; ++i)
if(x[i] == d) return 1;
return 0;
}
bool operator<(const node& b) const {
return ans > b.ans;
}
};
void bfs() {
priority_queue<node> q; q.push(node(0, 0, a[3], 0));
vis[0][0] = 1;
while(!q.empty()) {
node tt = q.top(), t; q.pop();
if(tt.reach()) { printf("%d %d\n", tt.ans, d); return; }
for(int i = 1; i <= 3; ++i) {
for(int j = 1; j <= 3; ++j) {
if(i == j) continue; t = tt;
if(t.canP(i,j)) { t.pour(i,j); if(!t.check()) q.push(t); }
}
}
}
printf("%d %d\n", ansp, dp);
}
int main() {
#ifdef _LOCAL
IN; //OT;
#endif // _LOCAL
int t; cin >> t;
while(t--) {
scanf("%d%d%d%d", &a[1], &a[2], &a[3], &d);
init(); bfs();
}
return 0;
}