Description
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 |
题意:给你三个杯子a,b,c;其中前两个是空的,第三个是满的;给的测试数据是每个杯子的容量,第四个是要求得的水的体积;若得不到,则取最接近的值;
输出时第一个数是水的转移量;第二个数是你所能得到的最接近所需要水的值;每次倒水每个杯子的状态都会变化,那么便可以考虑用广搜做;
eg:2 3 4 2
从第三个杯子倒水到第一个杯子;知道第一个杯子满了或者第三个杯子空了时在结束; 这是便得到所需要水的量 2 ; 其中水的转移量是 2 ;
那么便输出 2 2;
标程:
<span style="font-size:14px;"># include <cstdio> # include <queue> # include <cstring> # include <iostream> using namespace std; const int Max=202; int dist[Max][Max],ans[Max],cup[3]; //dist数组标记到这个状态使用了多少水;ans[ i ]用来记住得到 i 的水最佳转移量; struct node { int v[3],dist; bool operator < (const node &n) const { return dist > n.dist; } }; void updata(const node &u) //更新状态的水转移量; { for(int i=0;i<3;i++) { int d=u.v[i]; if(ans[d]<0||ans[d]>u.dist) ans[d]=u.dist; } } void solve(int a,int b,int c,int d) { cup[0]=a,cup[1]=b,cup[2]=c; //记住每个杯子的最大容量; node start; int i,j; start.v[0]=start.v[1]=0; start.v[2]=c; start.dist=0; memset(dist,-1,sizeof(dist)); memset(ans,-1,sizeof(ans)); priority_queue <node> q; q.push(start); dist[start.v[0]][start.v[1]]=0; while(!q.empty()) { node u; u=q.top(); q.pop(); updata(u); for(i=0;i<3;i++) for(j=0;j<3;j++) if(i!=j) { if(u.v[i]==0||u.v[j]==cup[j]) continue; //能不倒水 || 能不能盛水; int amost = cup[j] < u.v[i] + u.v[j] ? cup[j]-u.v[j] : u.v[i]; //求出转移量; node n; memcpy(&n,&u,sizeof(u)); n.dist+=amost; //更新新的状态; n.v[i]-=amost; n.v[j]+=amost; int &D = dist[n.v[0]][n.v[1]]; if(D < 0 || D > n.dist) //判断是不是出现过 || 用水量小于上一种情况,便重新入队列; { D = n.dist; q.push(n); } } } while(d>=0) //寻找最接近的值; { if(ans[d] < 0) d--; else { cout<<ans[d]<<' '<<d<<"\n"; return ; } } } int main() { int T; cin>>T; while(T--) { int a,b,c,d; cin>>a>>b>>c>>d; solve(a,b,c,d); } return 0; }</span>