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 |
Problem source: Bulgarian National Olympiad in Informatics 2003
Problem submitter: Ivaylo Riskov
Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury
题目大意:
有三个杯子它们的容量分别是a,b,c, 并且初始状态下第一个和第二个是空的, 第三个杯子是满水的。可以把一个杯子的水倒入另一个杯子,当然,当被倒的杯子满了或者倒的杯子水完了,就不能继续倒了。
你的任务是写一个程序计算出用最少的倒水量,使得其中一个杯子里有d升水。如果不能倒出d升水的话,那么找到一个d' < d ,使得d' 最接近d。
博客上的都是TLE,可能是UVA添加数据了,数据鞭策着代码向着更好的方向进发。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#define INF 999999999
#define min(A,B)(A<B?A:B)
using namespace std;
int maxx[4],d;
int ans[201];
bool v[201][201][201];
int flag;
struct node
{
int x;
int y;
int z;
int cnt;
};
bool operator < (const node &a, const node &b)
{
return a.cnt > b.cnt;
}
void BFS()
{
priority_queue<node>q;
node t,f;
flag = 0;
t.x = 0;
t.y = 0;
t.z = maxx[3];
t.cnt = 0;
v[t.x][t.y][t.z] = 1;
q.push(t);
while(!q.empty())
{
t = q.top();
q.pop();
ans[t.x] = min(ans[t.x],t.cnt);
ans[t.y] = min(ans[t.y],t.cnt);
ans[t.z] = min(ans[t.z],t.cnt);
int add;
if(t.x>0)
{
if(t.y < maxx[2])
{
add = maxx[2] - t.y;
if(add > t.x && !v[0][t.y+t.x][t.z])
{
f.x = 0;
f.y = t.y + t.x;
f.z = t.z;
f.cnt = t.cnt + t.x;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
else if(add <= t.x && !v[t.x-add][maxx[2]][t.z])
{
f.x = t.x - add;
f.y = maxx[2];
f.z = t.z;
f.cnt = t.cnt + add;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
}
if(t.z < maxx[3])
{
add = maxx[3] - t.z;
if(add > t.x && !v[0][t.y][t.z+t.x])
{
f.x = 0;
f.y = t.y;
f.z = t.z + t.x;
f.cnt = t.cnt + t.x;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
else if(add <= t.x && !v[t.x-add][t.y][maxx[3]])
{
f.x = t.x - add;
f.y = t.y;
f.z = maxx[3];
f.cnt = t.cnt + add;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
}
}
if(t.y>0)
{
if(t.z < maxx[3])
{
add = maxx[3] - t.z;
if(add > t.y && !v[t.x][0][t.z+t.y])
{
f.x = t.x;
f.y = 0;
f.z = t.z + t.y;
f.cnt = t.cnt + t.y;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
else if(add <= t.y && !v[t.x][t.y-add][maxx[3]])
{
f.x = t.x;
f.y = t.y - add;
f.z = maxx[3];
f.cnt = t.cnt + add;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
}
if(t.x < maxx[1])
{
add = maxx[1] - t.x;
if(add > t.y && !v[t.x+t.y][0][t.z])
{
f.x = t.x + t.y;
f.y = 0;
f.z = t.z;
f.cnt = t.cnt + t.y;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
else if(add <= t.y && !v[maxx[1]][t.y-add][t.z])
{
f.x = maxx[1];
f.y = t.y - add;
f.z = t.z;
f.cnt = t.cnt + add;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
}
}
if(t.z!=0)
{
if(t.x < maxx[1])
{
add = maxx[1] - t.x;
if(add > t.z && !v[t.x+t.z][t.y][0])
{
f.x = t.x + t.z;
f.y = t.y;
f.z = 0;
f.cnt = t.cnt + t.z;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
else if(add <= t.z && !v[maxx[1]][t.y][t.z-add])
{
f.x = maxx[1];
f.y = t.y;
f.z = t.z - add;
f.cnt = t.cnt + add;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
}
if(t.y < maxx[2])
{
add = maxx[2] - t.y;
if(add > t.z && !v[t.x][t.y+t.z][0])
{
f.x = t.x;
f.y = t.y + t.z;
f.z = 0;
f.cnt = t.cnt + t.z;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
else if(add <= t.z && !v[t.x][maxx[2]][t.z-add])
{
f.x = t.x;
f.y = maxx[2];
f.z = t.z - add;
f.cnt = t.cnt + add;
v[f.x][f.y][f.z] = 1;
q.push(f);
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&maxx[1],&maxx[2],&maxx[3],&d);
for(int i=0; i<=maxx[1]; i++)
{
for(int j=0; j<=maxx[2]; j++)
{
for(int k=0; k<=maxx[3]; k++)
{
v[i][j][k] = 0;
}
}
}
for(int i=0; i<=d; i++)
{
ans[i] = INF;
}
BFS();
for(int i=d; i>=0; i--)
{
if(ans[i]<INF)
{
printf("%d %d\n",ans[i],i);
break;
}
}
}
return 0;
}