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。

  1. #include <cstdio>
  2. #include <queue>
  3. #define min(A,B)(A<B?A:B)
  4. #define INF 999999999
  5. using namespace std;
  6.  
  7. struct S{
  8. int x,y,z,sum;
  9.  
  10. bool operator<(const S &p) const
  11. {
  12. return sum>p.sum;
  13. }
  14.  
  15. }t;
  16.  
  17. int a,b,c,d,ans[201];
  18. bool vis[201][201][201];
  19.  
  20. int main()
  21. {
  22. int T,i,j,k,x,y,z,sum;
  23.  
  24. scanf("%d",&T);
  25.  
  26. while(T--)
  27. {
  28. scanf("%d%d%d%d",&a,&b,&c,&d);
  29.  
  30. for(i=0;i<=a;i++) for(j=0;j<=b;j++) for(k=0;k<=c;k++) vis[i][j][k]=0;
  31.  
  32. for(i=0;i<=d;i++) ans[i]=INF;
  33.  
  34. vis[0][0][c]=1;
  35.  
  36. t.x=0;
  37. t.y=0;
  38. t.z=c;
  39. t.sum=0;
  40.  
  41. priority_queue<S>que;
  42.  
  43. que.push(t);
  44.  
  45. while(!que.empty())
  46. {
  47. t=que.top();
  48. que.pop();
  49.  
  50. x=t.x;
  51. y=t.y;
  52. z=t.z;
  53. sum=t.sum;
  54.  
  55. ans[x]=min(ans[x],sum);
  56. ans[y]=min(ans[y],sum);
  57. ans[z]=min(ans[z],sum);
  58.  
  59. if(x)
  60. {
  61. if(y<b)
  62. {
  63. if(x>=b-y && !vis[x-b+y][b][z])
  64. {
  65. t.x=x-b+y;
  66. t.y=b;
  67. t.z=z;
  68. t.sum=sum+b-y;
  69.  
  70. vis[x-b+y][b][z]=1;
  71.  
  72. que.push(t);
  73. }
  74. else if(x<b-y && !vis[0][y+x][z])
  75. {
  76. t.x=0;
  77. t.y=y+x;
  78. t.z=z;
  79. t.sum=sum+x;
  80.  
  81. vis[0][y+x][z]=1;
  82.  
  83. que.push(t);
  84. }
  85. }
  86.  
  87. if(z<c)
  88. {
  89. if(x>=c-z && !vis[x-c+z][y][c])
  90. {
  91. t.x=x-c+z;
  92. t.y=y;
  93. t.z=c;
  94. t.sum=sum+c-z;
  95.  
  96. vis[x-c+z][y][c]=1;
  97.  
  98. que.push(t);
  99. }
  100. else if(x<c-z && !vis[0][y][z+x])
  101. {
  102. t.x=0;
  103. t.y=y;
  104. t.z=z+x;
  105. t.sum=sum+x;
  106.  
  107. vis[0][y][z+x]=1;
  108.  
  109. que.push(t);
  110. }
  111. }
  112. }
  113.  
  114. if(y)
  115. {
  116. if(x<a)
  117. {
  118. if(y>=a-x && !vis[a][y-a+x][z])
  119. {
  120. t.x=a;
  121. t.y=y-a+x;
  122. t.z=z;
  123. t.sum=sum+a-x;
  124.  
  125. vis[a][y-a+x][z]=1;
  126.  
  127. que.push(t);
  128. }
  129. else if(y<a-x && !vis[x+y][0][z])
  130. {
  131. t.x=x+y;
  132. t.y=0;
  133. t.z=z;
  134. t.sum=sum+y;
  135.  
  136. vis[x+y][0][z]=1;
  137.  
  138. que.push(t);
  139. }
  140. }
  141.  
  142. if(z<c)
  143. {
  144. if(y>=c-z && !vis[x][y-c+z][c])
  145. {
  146. t.x=x;
  147. t.y=y-c+z;
  148. t.z=c;
  149. t.sum=sum+c-z;
  150.  
  151. vis[x][y-c+z][c]=1;
  152.  
  153. que.push(t);
  154. }
  155. else if(y<c-z && !vis[x][0][z+y])
  156. {
  157. t.x=x;
  158. t.y=0;
  159. t.z=z+y;
  160. t.sum=sum+y;
  161.  
  162. vis[x][0][z+y]=1;
  163.  
  164. que.push(t);
  165. }
  166. }
  167. }
  168.  
  169. if(z)
  170. {
  171. if(x<a)
  172. {
  173. if(z>=a-x && !vis[a][y][z-a+x])
  174. {
  175. t.x=a;
  176. t.y=y;
  177. t.z=z-a+x;
  178. t.sum=sum+a-x;
  179.  
  180. vis[a][y][z-a+x]=1;
  181.  
  182. que.push(t);
  183. }
  184. else if(z<a-x && !vis[x+z][y][0])
  185. {
  186. t.x=x+z;
  187. t.y=y;
  188. t.z=0;
  189. t.sum=sum+z;
  190.  
  191. vis[x+z][y][0]=1;
  192.  
  193. que.push(t);
  194. }
  195. }
  196.  
  197. if(y<b)
  198. {
  199. if(z>=b-y && !vis[x][b][z-b+y])
  200. {
  201. t.x=x;
  202. t.y=b;
  203. t.z=z-b+y;
  204. t.sum=sum+b-y;
  205.  
  206. vis[x][b][z-b+y]=1;
  207.  
  208. que.push(t);
  209. }
  210. else if(z<b-y && !vis[x][y+z][0])
  211. {
  212. t.x=x;
  213. t.y=y+z;
  214. t.z=0;
  215. t.sum=sum+z;
  216.  
  217. vis[x][y+z][0]=1;
  218.  
  219. que.push(t);
  220. }
  221. }
  222. }
  223. }
  224.  
  225. for(i=d;;i--)
  226. {
  227. if(ans[i]<INF)
  228. {
  229. printf("%d %d\n",ans[i],i);
  230.  
  231. break;
  232. }
  233. }
  234. }
  235. }

UVA-10603-Fill(BFS+优先队列)的更多相关文章

  1. UVA 10603 - Fill BFS~

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

  2. UVa 10603 Fill (BFS && 经典模拟倒水 && 隐式图)

    题意 : 有装满水的6升的杯子.空的3升杯子和1升杯子,3个杯子中都没有刻度.不使用道具情况下,是否可量出4升水呢? 你的任务是解决一般性的问题:设3个杯子的容量分别为a, b, c,最初只有第3个杯 ...

  3. UVa 10603 Fill [暴力枚举、路径搜索]

    10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive intege ...

  4. UVa 10603 Fill (暴力BFS+优先队列)

    题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和d ...

  5. UVA - 10603 Fill(BFS求最小值问题)

    题目: 给出三个杯子(没有刻度线)的容量,起初之后第三个杯子是满的,其他的两个杯子是空的,容量分别是a.b.c.问最少需要倒多少升水才能让某一个杯子中的水有d升?如果不能恰好做到d升,就让某一个杯子里 ...

  6. UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...

  7. 【路径寻找问题】UVa 10603 - Fill

    如家大神书上的例题.第一次接触也是按代码敲得.敲的过程感觉很直观.但自己写估计会写的乱七八糟.以后不能砍得难就不愿意做这种题.否则只能做一些水题了.(PS:48) 紫书 #include<ios ...

  8. UVA 10603 Fill

    题意: 题目的意思是倒水,给出的四个数据是第一个水杯,第二个水杯,第三个水杯,和目标水量.一开始只有第三个水杯是满的,剩下的水杯是空的.倒水的时候只能把倒水出来的这个杯子倒空,或是倒水进去的杯子倒满. ...

  9. UVA - 10603 Fill(隐式图搜索)

    题目大意:经典的倒水问题. 给你三个瓶子,体积为a,b,c. 刚開始a.b是空的,c是满的,如今要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多 ...

  10. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

随机推荐

  1. bzoj 1089 SCOI2003严格n元树 递推

    挺好想的,就是一直没调过,我也不知道哪儿的错,对拍也拍了,因为数据范围小,都快手动对拍了也不知道 哪儿错了.... 我们定义w[i]代表深度<=i的严格n元树的个数 那么最后w[d]-w[d-1 ...

  2. IE 6 position不支持fixed属性的解决方案

    抛出另一个问题:IE7已经支持position:fixed了,而IE6却不支持,解决这个问题的办法如下: 现在有一个元素的id是element,它需要实现fixed效果,我们既想要它在正常的浏览器下使 ...

  3. Jackson对多态和多子类序列化的处理配置

    目录 Jackson 多态类型的处理 Jackson Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 多态类型的处理 jacks ...

  4. [NOIP 2010] 引水入城

    搜索+贪心. 参考博客:http://blog.sina.com.cn/s/blog_8442ec3b0100xib1.html 主要是要看出来,如果有解的话,每个沿湖城市能够流到的范围是连续的区间. ...

  5. Mac自带的SSH客户端

    https://segmentfault.com/q/1010000002806469 还能设置连接成持久连接,方便使用: ttps://www.zhihu.com/question/20541129 ...

  6. java 生成execl下载

    /** * execl Export */ private void createExecl(HttpServletRequest request, HttpServletResponse respo ...

  7. flask+gunicorn中文文件下载报错问题及解决

    导言 问题源起与一个静态文件下载的接口: from flask import Flask, current_app app = Flask(__name__) @app.route('/file_na ...

  8. vue + vue-router + vue-resource 基于vue-cli脚手架 --->笔记

    ps: 基于Vue2.0 npm的vue-cli脚手架 在vue-router中路由路径的简写代码: 点击打开项目 > build > webpack.base.conf.js 找到web ...

  9. resin + eclipse 遇到的问题

    1. 编译jsp报错: com.caucho.jsp.JspParseException: javac compiler is not available in Java(TM) SE Runtime ...

  10. selenium TestNG基本注释和属性

    TestNG注释详解 suite 属性说明: @name: suite 的名称,必须参数@junit:是否以Junit 模式运行,可选值(true | false),默认"false&quo ...