Fill,fillin
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>