首页 > 代码库 > UVa10603 倒水 Fill-状态空间搜索

UVa10603 倒水 Fill-状态空间搜索

https://vjudge.net/problem/UVA-10603

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 2 2 3 4 2 96 97 199 62

Sample Output 2 2 9859 62

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 using namespace std; 6 const int maxn=201; 7 struct Node{ 8     int x,y,z; 9     int water;10     bool operator<(const Node& b)const{11         return water>b.water;12     }13 };14 int a,b,c,d,ans[maxn],done[maxn][maxn];15 void init(){16     memset(ans,-1,sizeof(ans));17     memset(done,0,sizeof(done));18 }19 int main()20 {21     int T;22     scanf("%d",&T);23     while(T--){24         priority_queue<Node> Q;25         init();26         scanf("%d %d %d %d",&a,&b,&c,&d);27         Node start=(Node){0,0,c,0};28         Q.push(start);29         while(!Q.empty()){30             Node r=Q.top();Q.pop();31             if(ans[r.x]<0||r.water<ans[r.x]) ans[r.x]=r.water;32             if(ans[r.y]<0||r.water<ans[r.y]) ans[r.y]=r.water;33             if(ans[r.z]<0||r.water<ans[r.z]) ans[r.z]=r.water;34             done[r.x][r.y]=1;35             if(ans[d]>=0) break;36             int change;37             change=min(r.x,b-r.y);38             if(change&&!done[r.x-change][r.y+change]) Q.push((Node){r.x-change,r.y+change,r.z,r.water+change});39             change=min(r.x,c-r.z);40             if(change&&!done[r.x-change][r.y]) Q.push((Node){r.x-change,r.y,r.z+change,r.water+change});41             change=min(r.y,a-r.x);42             if(change&&!done[r.x+change][r.y-change]) Q.push((Node){r.x+change,r.y-change,r.z,r.water+change});43             change=min(r.y,c-r.z);44             if(change&&!done[r.x][r.y-change]) Q.push((Node){r.x,r.y-change,r.z+change,r.water+change});45             change=min(r.z,a-r.x);46             if(change&&!done[r.x+change][r.y]) Q.push((Node){r.x+change,r.y,r.z-change,r.water+change});47             change=min(r.z,b-r.y);48             if(change&&!done[r.x][r.y+change]) Q.push((Node){r.x,r.y+change,r.z-change,r.water+change});49         }50         while(d>=0){51             if(ans[d]>=0){52                 printf("%d %d\n",ans[d],d);53                 break;54             }55             --d;56         }57     }58     return 0;59 }

 

UVa10603 倒水 Fill-状态空间搜索