#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = 210;
int a, b, c, d;
struct Node{
int v[3], dis;
bool operator < (const Node& rhs) const {
return dis > rhs.dis;
}
};
int vis[MAXN][MAXN];
int cup[3];
int ans[MAXN];
void update_ans(const Node& n) {
for(int i = 0; i < 3; i++) {
int d = n.v[i];
if(ans[d] < 0 || ans[d] > n.dis) ans[d] = n.dis;
}
}
void solve(int a, int b, int c, int d) {
Node n0;
cup[0] = a; cup[1] = b; cup[2] = c;
n0.v[0] = n0.v[1] = 0;
n0.v[2] = c; n0.dis = 0;
update_ans(n0);
priority_queue<Node> q;
q.push(n0);
vis[0][0] = 1;
while(!q.empty()) {
if(ans[d] >= 0) break;
Node n = q.top(); q.pop();
update_ans(n);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(i != j) {
if(n.v[i] == 0 || n.v[j] == cup[j]) continue;
int amount = min(cup[j] - n.v[j], n.v[i]);
Node n1;
memcpy(&n1, &n, sizeof(n));
n1.v[i] -= amount;
n1.v[j] += amount;
if(vis[n1.v[0]][n1.v[1]]) continue;
vis[n1.v[0]][n1.v[1]] = true;
n1.dis = n.dis + amount;
q.push(n1);
}
}
}
}
while(d >= 0) {
if(ans[d] >= 0) { printf("%d %d\n", ans[d], d); return ;}
d--;
}
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
memset(ans, -1, sizeof(ans));
memset(vis, 0, sizeof(vis));
scanf("%d%d%d%d", &a, &b, &c, &d);
solve(a, b, c, d);
}
return 0;
}
/*
2
2 3 4 2
96 97 199 62
*/
UVA10603Fill
最新推荐文章于 2024-04-04 03:17:18 发布