#include<iostream>
#include<string>
#include<cassert>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
using namespace std;
struct Node {
int v[3], dist;
bool operator<(const Node& rhs)const
{
return dist > rhs.dist;
}
};
const int maxn = 200 + 5;
int ans[maxn];
int cap[3];
int vis[maxn][maxn];
void update_ans(Node& u)
{
for (int i = 0; i < 3; ++i)
{
int d = u.v[i];
if (ans[d] < 0 || u.dist < ans[d])
ans[d] = u.dist;
}
}
void solve(int a, int b, int c, int d)
{
cap[0] = a; cap[1] = b; cap[2] = c;
Node u;
u.dist = 0;
u.v[0] = u.v[1] = 0;
u.v[2] = c;
priority_queue<Node>q;
memset(vis, 0, sizeof(vis));
memset(ans, -1, sizeof(ans));
q.push(u);
vis[0][0] = 1;
while (!q.empty())
{
Node t = q.top(); q.pop();
update_ans(t);
if (ans[d] >= 0)
break;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (i != j&&t.v[i]!=0&&t.v[j]!=cap[j])
{
int amount = min(t.v[i] + t.v[j], cap[j]) - t.v[j];
Node t2;
memcpy(&t2, &t, sizeof(t2));
t2.v[i] -= amount;
t2.v[j] += amount;
t2.dist += amount;
if (!vis[t2.v[0]][t2.v[1]])
{
vis[t2.v[0]][t2.v[1]] = 1;
q.push(t2);
}
}
}
}
}
while (d >= 0)
{
if (ans[d] >= 0)
{
cout << ans[d] << " " << d << endl;
return;
}
--d;
}
}
int main()
{
//freopen("input.txt", "r", stdin);// freopen("output2.txt", "w", stdout);
int kase;
cin >> kase;
int a, b, c, d;
while (kase-- > 0)
{
cin >> a >> b >> c >> d;
solve(a, b, c, d);
}
}
紫书UVa10603
最新推荐文章于 2024-06-12 10:00:23 发布