UVA-10603-Fill (BFS + priority queue), uva-10603-fillbfs
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 med 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 shocould find a smaller amount of water d' <d which is closest to d and for which d' liters cocould be produced. when d 'is found, your program shocould 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 cocould 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 |
Train of Thought: Mark the amount of water in three cups and use BFS in the priority queue.
#include <cstdio>#include <queue>#define min(A,B)(A<B?A:B)#define INF 999999999using namespace std;struct S{int x,y,z,sum;bool operator<(const S &p) const{ return sum>p.sum;}}t;int a,b,c,d,ans[201];bool vis[201][201][201];int main(){ int T,i,j,k,x,y,z,sum; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&a,&b,&c,&d); for(i=0;i<=a;i++) for(j=0;j<=b;j++) for(k=0;k<=c;k++) vis[i][j][k]=0; for(i=0;i<=d;i++) ans[i]=INF; vis[0][0][c]=1; t.x=0; t.y=0; t.z=c; t.sum=0; priority_queue<S>que; que.push(t); while(!que.empty()) { t=que.top(); que.pop(); x=t.x; y=t.y; z=t.z; sum=t.sum; ans[x]=min(ans[x],sum); ans[y]=min(ans[y],sum); ans[z]=min(ans[z],sum); if(x) { if(y<b) { if(x>=b-y && !vis[x-b+y][b][z]) { t.x=x-b+y; t.y=b; t.z=z; t.sum=sum+b-y; vis[x-b+y][b][z]=1; que.push(t); } else if(x<b-y && !vis[0][y+x][z]) { t.x=0; t.y=y+x; t.z=z; t.sum=sum+x; vis[0][y+x][z]=1; que.push(t); } } if(z<c) { if(x>=c-z && !vis[x-c+z][y][c]) { t.x=x-c+z; t.y=y; t.z=c; t.sum=sum+c-z; vis[x-c+z][y][c]=1; que.push(t); } else if(x<c-z && !vis[0][y][z+x]) { t.x=0; t.y=y; t.z=z+x; t.sum=sum+x; vis[0][y][z+x]=1; que.push(t); } } } if(y) { if(x<a) { if(y>=a-x && !vis[a][y-a+x][z]) { t.x=a; t.y=y-a+x; t.z=z; t.sum=sum+a-x; vis[a][y-a+x][z]=1; que.push(t); } else if(y<a-x && !vis[x+y][0][z]) { t.x=x+y; t.y=0; t.z=z; t.sum=sum+y; vis[x+y][0][z]=1; que.push(t); } } if(z<c) { if(y>=c-z && !vis[x][y-c+z][c]) { t.x=x; t.y=y-c+z; t.z=c; t.sum=sum+c-z; vis[x][y-c+z][c]=1; que.push(t); } else if(y<c-z && !vis[x][0][z+y]) { t.x=x; t.y=0; t.z=z+y; t.sum=sum+y; vis[x][0][z+y]=1; que.push(t); } } } if(z) { if(x<a) { if(z>=a-x && !vis[a][y][z-a+x]) { t.x=a; t.y=y; t.z=z-a+x; t.sum=sum+a-x; vis[a][y][z-a+x]=1; que.push(t); } else if(z<a-x && !vis[x+z][y][0]) { t.x=x+z; t.y=y; t.z=0; t.sum=sum+z; vis[x+z][y][0]=1; que.push(t); } } if(y<b) { if(z>=b-y && !vis[x][b][z-b+y]) { t.x=x; t.y=b; t.z=z-b+y; t.sum=sum+b-y; vis[x][b][z-b+y]=1; que.push(t); } else if(z<b-y && !vis[x][y+z][0]) { t.x=x; t.y=y+z; t.z=0; t.sum=sum+z; vis[x][y+z][0]=1; que.push(t); } } } } for(i=d;;i--) { if(ans[i]<INF) { printf("%d %d\n",ans[i],i); break; } } }}
Width-first search (BFS), you can give a code framework, by the way, to describe ideas ,,
Breadth-first search (BFS): a simple and easy-to-use search method, usually used in combination with queues or priority queues. Algorithm idea: Starting from one point, the vertex to be searched is put into the queue, the search vertex is popped up, and then the vertex to be searched is put into the queue, so as to achieve priority search in the breadth direction. For the width-first search method, the structure of the State node and the node expansion rule are different for different problems, but the search policy is the same, so the algorithm framework is basically the same. Struct tnode {// define the data type of a node ...... // Determine the required data type based on the specific problem} state [maxn]; // define an array of the tnode type as the queue void init () of the storage node (); // initialize the function bool extend (); // determine whether the node can be extended. If yes, a new node bool repeat () is generated (); // check whether bool find () has been found on the new node in the queue. // check whether the new node is the target node void outs (); // output the node status void printpath (); // output path // jsonint main () {init (); bfs () ;}// jsonvoid init () {// initialization} // jsonvoid bfs () // BFS algorithm main program {tnode temp; // tnode temporary node int head = 0, tail = 0; // queue head pointer and tail pointer while (head <= tail & tail <maxn) {// The queue is not empty and the data space is used up in a cycle // determine a node Extension Rule temp = state [head] based on the specific problem; // get the node of the queue header if (extend ())...... remaining full text>
Hdu hangdian acm 1026 Ignatius and the Princess I, A BFS Daniel Error Correction
What's wrong, boss? Isn't it AC? =. =?