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
解决方案:水杯倒水的情况可以形成一个有向图,可用bfs,用一个二位数组来标记,避免重判。但这题要注意一个地方,它要求总的倒水量多最少,不是倒水次数,为了这个卡了一上午。。。。。。
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
bool vis[210][210];
struct cup{
int a[3],times;
bool operator <(const cup & x)const
{
return times>x.times;
}
};
int a[3],d,D,Min,_min;
void bfs(){
memset(vis,false,sizeof(vis));
cup t;
t.a[0]=0;t.a[1]=0;t.a[2]=a[2];
vis[0][0]=true;
t.times=0;
priority_queue<cup> Q;///用优先队列,可保证先从倒水量最少的开始搜
Q.push(t);
_min=-1;
// int head=0,tail=1;
// bool flag=true;
while(!Q.empty()){
cup temp=Q.top();
Q.pop();
cup cur=temp;
for(int i=0;i<3;i++){
if(temp.a[i]==d){
_min=temp.times;
return ;
}
}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
temp=cur;
if(i!=j&&temp.a[i]!=0&&temp.a[j]!=a[j]){
if(temp.a[i]<=(a[j]-temp.a[j])){///i杯被倒空的情况
int p=temp.a[i];
temp.a[j]+=temp.a[i];
temp.a[i]=0;
if(!vis[temp.a[0]][temp.a[1]]){
vis[temp.a[0]][temp.a[1]]=true;
temp.times+=p;
Q.push(temp);
}
}
else {///j杯被倒满的情况
int p=(a[j]-temp.a[j]);
temp.a[i]-=(a[j]-temp.a[j]);
temp.a[j]=a[j];
if(!vis[temp.a[0]][temp.a[1]]){
vis[temp.a[0]][temp.a[1]]=true;
temp.times+=p;
Q.push(temp);
}
}
}
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&a[0],&a[1],&a[2],&d);
bfs();
while(_min==-1)
{ d--;
bfs();
}
printf("%d %d\n",_min,d);
}
return 0;
}