UVa 10603 Fill [暴力枚举、路径搜索]
10603 Fill
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 rst and the second jug are initially empty, while the third is completely lled with water. It is allowed to pour water from one jug into another until either the rst 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 nd 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 rst 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 rst 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
2342
96 97 199 62
Sample Output
2 2
9859 62
解题思路:
1.将两个水罐中水量(a,b)作为状态量 ,枚举所有状态,共有201*201=40401种情况。
2.每次取倒水量最小的状态展开,因此采用优先队列(priority_queue)进行存储,在state结构类中定义静态成员函数‘<’;
3.记录每个状态需要的最小倒水量。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; const int maxn=+; struct state{
int v[]={},dist=;
bool operator <(const state& u)const {
return dist>u.dist;
}
}; int vis[maxn][maxn],jug[],ans[maxn],d; void update_ans(const state& u){
for(int i=;i<;i++){
int d=u.v[i];
if(ans[d]==-||ans[d]>u.dist)
ans[d]=u.dist;
}
} void solve(int d){
memset(vis,,sizeof vis);
memset(ans,-,sizeof ans);
priority_queue<state> q;
state start;
start.v[]=start.v[]=;start.v[]=jug[];
start.dist=;
q.push(start);
vis[][]=;
while(!q.empty()){
state u=q.top();q.pop();
update_ans(u);
if(ans[d]>=) break;
for(int i=;i<;i++)
for(int j=;j<;j++)
if(i!=j){
if(u.v[i]>&&u.v[j]<jug[j]){
int mount=min(jug[j],u.v[i]+u.v[j])-u.v[j];
state u2;
memcpy(&u2,&u,sizeof u);
u2.v[i]-=mount;u2.v[j]+=mount;u2.dist+=mount;
if(!vis[u2.v[]][u2.v[]]){ vis[u2.v[]][u2.v[]]=;
q.push(u2);
}
} }
}
while(d>=){
if(ans[d]>=){
printf("%d %d\n",ans[d],d);
return ;
}
d--;
}
}
int main() {
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&jug[],&jug[],&jug[],&d);
solve(d);
}
return ;
}
UVa 10603 Fill [暴力枚举、路径搜索]的更多相关文章
- UVa 10603 Fill (暴力BFS+优先队列)
题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和d ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- UVa 1354 Mobile Computing[暴力枚举]
**1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...
- UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...
- Uva 10167 - Birthday Cake 暴力枚举 随机
Problem G. Birthday Cake Background Lucy and Lily are twins. Today is their birthday. Mother buys ...
- UVA 725 division【暴力枚举】
[题意]:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出“There are no solut ...
- UVA 10603 - Fill BFS~
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...
- 【路径寻找问题】UVa 10603 - Fill
如家大神书上的例题.第一次接触也是按代码敲得.敲的过程感觉很直观.但自己写估计会写的乱七八糟.以后不能砍得难就不愿意做这种题.否则只能做一些水题了.(PS:48) 紫书 #include<ios ...
- UVA 10603 Fill
题意: 题目的意思是倒水,给出的四个数据是第一个水杯,第二个水杯,第三个水杯,和目标水量.一开始只有第三个水杯是满的,剩下的水杯是空的.倒水的时候只能把倒水出来的这个杯子倒空,或是倒水进去的杯子倒满. ...
随机推荐
- 网页性能测试之WebPageTest
想知道您的网站,性能怎么样? 很自然,首先得找一个广被认可的测试工具.我们推荐WebPageTest: WebPageTest 它是google 开源项目”make the web faster “的 ...
- 【JZOJ4816】【NOIP2016提高A组五校联考4】label
题目描述 输入 输出 样例输入 3 2 2 0 1 2 3 3 2 1 3 1 2 3 3 1 1 2 2 3 样例输出 4 2 12 数据范围 样例解释 解法 设f[i][j]为在第i个点填了j的合 ...
- 【JZOJ4810】【NOIP2016提高A组五校联考1】道路规划
题目描述 输入 输出 样例输入 5 1 4 5 2 3 3 4 2 1 5 样例输出 3 数据范围 样例解释 解法 模型显然. 设第一列为a[],第二列为b[],f[i]为前i个数的最大答案. 顺序枚 ...
- 【风马一族_php】常用的语句
设置脚本的编码 <?php header('Content-type:text/html;charset=utf-8'); ?> 按原格式的输入内容 echo <pre>; ...
- 为数据计算提供强力引擎,阿里云文件存储HDFS v1.0公测发布
在2019年3月的北京云栖峰会上,阿里云正式推出全球首个云原生HDFS存储服务—文件存储HDFS,为数据分析业务在云上提供可线性扩展的吞吐能力和免运维的快速弹性伸缩能力,降低用户TCO.阿里云文件存储 ...
- PHP Laravel系列之环境搭建( VirtualBox+Vagrant+Homestead+系列网址)
搭建环境从来都是阻挡一门新技能的最致命的硬伤,为了这个环境,我又是花费了半天的时间,各种问题层出不穷,下面基于网上的一些教程(我看到的都多少有些问题) 开始的时候是在实验楼这个平台上开始学习的,不过 ...
- iOS开发周报-- 第一期
从Java转iOS第一个项目总结 http://www.cocoachina.com/ios/20150417/11595.html icon设计探讨:图标,文字,还是图标加文字? http://ww ...
- 【NS2】trace 文件格式(转载)
本文档是对 http://nsnam.isi.edu/nsnam/index.php/NS-2_Trace_Formats > 的翻译. 译注:本文描述的无线格Trace格式已经有些陈旧,现在一 ...
- [软考]之软件过程模型II 标签: 软件工程 2015-11-01 11:52 1612人阅读 评论(22) 收
上一篇博客总结了瀑布模型/V模型/增量模型这三种软件模型,然而我们还有一个很重要的问题忘了回答,那就是,什么是软件过程模型? 什么是软件过程模型? 软件过程是软件开发与维护的工作流程和工艺流程,是软件 ...
- Streamy 使用RDBMS