There are three jugs with a volume of a, band c liters. (a, b, and c are positive integers not greater than 200). Thefirst and the second jug are initially empty, while the third
is completely filled with water. It isallowed to pour water from one jug into another until either the first one isempty or the second one is full. This operation can be performed zero, one ormore times.
You are to write a program that computesthe least total amount of water that needs to be poured; so that at least oneof the jugs contains exactly d liters of water (d is a positive integer notgreater than 200). If it is not possible to measure d liters this way yourprogram should find a smaller amount of water d' < d which is closest to dand for which d' liters could be produced. When d' is found, your programshould 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 numberof test cases. In the next T lines, T test cases follow. Each test case isgiven in one line of input containing four space separated integers - a, b, cand d.
Output
The output consists of two integersseparated 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. Thesecond integer equals d, if d liters of water could be produced by suchtransformations, or equals the closest smaller value d' that your program hasfound.
Sample Input | Sample Output |
2 2 3 4 2 96 97 199 62 | 2 2 9859 62 |
代码:
// UVa10603 Fill
// Rujia Liu
#include<cstdio>
#include<cstring>
#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 mark[maxn][maxn], dist[maxn][maxn],cap[3], ans[maxn];
void update_ans(const 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;
memset(ans, -1, sizeof(ans));
memset(mark, 0, sizeof(mark));
memset(dist, -1, sizeof(dist));
priority_queue<Node> q;
Node start;
start.dist = 0;
start.v[0] = 0;
start.v[1] = 0;
start.v[2] = c;
q.push(start);
dist[0][0] = 0;
while(!q.empty())
{
Node u = q.top();
q.pop();
if(mark[u.v[0]][u.v[1]]) continue;
mark[u.v[0]][u.v[1]] = 1;
update_ans(u);
if(ans[d] >= 0) break;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++) if(i != j)
{
if(u.v[i] == 0 || u.v[j] ==cap[j]) continue;
int amount = min(cap[j],u.v[i] + u.v[j]) - u.v[j];
Node u2;
memcpy(&u2, &u,sizeof(u));
u2.dist = u.dist + amount;
u2.v[i] -= amount;
u2.v[j] += amount;
int& D =dist[u2.v[0]][u2.v[1]];
if(D < 0 || u2.dist <D)
{
D = u2.dist;
q.push(u2);
}
}
}
while(d >= 0)
{
if(ans[d] >= 0)
{
printf("%d %d\n", ans[d], d);
return;
}
d--;
}
}
int main()
{
int T, a, b, c, d;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d%d", &a, &b, &c, &d);
solve(a, b, c, d);
}
return 0;
}