设为首页 加入收藏

TOP

UVA-10603-Fill(BFS+优先队列)
2015-07-20 17:46:02 来源: 作者: 【 】 浏览:1
Tags:UVA-10603-Fill BFS 优先 队列

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

思路:标记三个杯子的水量,用优先队列的BFS。


#include 
  
   
#include 
   
     #define min(A,B)(A
    
     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
     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-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
       
        =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
        
         =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
         
          =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
          
           =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
           
            =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
            
             

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 2039 To and Fro(水题) 下一篇UVA - 1356 Bridge

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中如何将结构体 (2025-12-24 22:20:09)
·纯C语言结构体成员变 (2025-12-24 22:20:06)
·C语言中,指针函数和 (2025-12-24 22:20:03)
·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)