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 <cstdio>
- #include <queue>
- #define min(A,B)(A<B?A:B)
- #define INF 999999999
- using 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;
- }
- }
- }
- }
UVA-10603-Fill(BFS+优先队列)的更多相关文章
- UVA 10603 - Fill BFS~
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...
- UVa 10603 Fill (BFS && 经典模拟倒水 && 隐式图)
题意 : 有装满水的6升的杯子.空的3升杯子和1升杯子,3个杯子中都没有刻度.不使用道具情况下,是否可量出4升水呢? 你的任务是解决一般性的问题:设3个杯子的容量分别为a, b, c,最初只有第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 ...
- UVa 10603 Fill (暴力BFS+优先队列)
题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和d ...
- UVA - 10603 Fill(BFS求最小值问题)
题目: 给出三个杯子(没有刻度线)的容量,起初之后第三个杯子是满的,其他的两个杯子是空的,容量分别是a.b.c.问最少需要倒多少升水才能让某一个杯子中的水有d升?如果不能恰好做到d升,就让某一个杯子里 ...
- UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...
- 【路径寻找问题】UVa 10603 - Fill
如家大神书上的例题.第一次接触也是按代码敲得.敲的过程感觉很直观.但自己写估计会写的乱七八糟.以后不能砍得难就不愿意做这种题.否则只能做一些水题了.(PS:48) 紫书 #include<ios ...
- UVA 10603 Fill
题意: 题目的意思是倒水,给出的四个数据是第一个水杯,第二个水杯,第三个水杯,和目标水量.一开始只有第三个水杯是满的,剩下的水杯是空的.倒水的时候只能把倒水出来的这个杯子倒空,或是倒水进去的杯子倒满. ...
- UVA - 10603 Fill(隐式图搜索)
题目大意:经典的倒水问题. 给你三个瓶子,体积为a,b,c. 刚開始a.b是空的,c是满的,如今要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
随机推荐
- CDLinux 自动休眠功能的关闭方法
CDLinux 自动休眠功能的关闭方法: 控制台下使用xset命令来完成. xset q 可以查看当前屏幕保护和电源管理的状态信息 具体设置时,常用的有以下参数: xset s //这个参数设置屏 ...
- 如何开启SUSE Linux的SSH
1. 使用命令行停止防火墙 /etc/init.d/SuSEfirewall2_init stop /etc/init.d/SuSEfirewall2_setup stop 2. 修改 vi /etc ...
- mysql 四 表操作
表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为一条记录 本节重点: 1 创建 ...
- onvif实现
前言 负责开发了公司的onvif,一个人从0开始写的,花了两个月 !!!下面是我的总结. onvif介绍 ONVIF[Open Network Video Interface Forum](开放型网络 ...
- UVALIVE 3486 Cells
通过入栈出栈顺序判断祖先关系 这里UVALIVE还 #include <map> #include <set> #include <list> #include & ...
- 1.flask视图和URL
1.第一个flask程序 from flask import Flask ''' Flask这个类是项目的核心,以后很多操作都是基于这个类的对象 注册URL等等,都是基于这个类 ''' app = F ...
- 我们应选择怎样的IT公司
最近经常有朋友提问,同时收到几家公司的offer,应该如何选择,或者找工作的时候,找怎样的公司,我在这里阐述一下我的观点.但愿对朋友们有所帮助. 还是那句老话,选择什么样的公司,关键是你想要过什么样的 ...
- C# 连接和操作SQL SERVER数据库
用C#sqlserver实现增删改查http://www.worlduc.com/blog2012.aspx?bid=730767 using System.Data;using System.Dat ...
- [BZOJ1475]方格取数 网络流 最小割
1475: 方格取数 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 512[Submit][Status][Discuss] ...
- SecureCRT 的上传和下载操作
在网上找了两篇文章,分别关于ftp和ssh的上传下载,如果有好的大家可以留言分享,不胜感谢~ 因为关于ftp的比较少,就copy上面,本人并没有验证.关于ssh用sr和sz发现一条错误,而且网上也有解 ...