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 [暴力枚举、路径搜索]的更多相关文章

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

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

  2. UVA.12716 GCD XOR (暴力枚举 数论GCD)

    UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...

  3. UVa 1354 Mobile Computing[暴力枚举]

    **1354 Mobile Computing** There is a mysterious planet called Yaen, whose space is 2-dimensional. Th ...

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

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

  5. Uva 10167 - Birthday Cake 暴力枚举 随机

      Problem G. Birthday Cake Background Lucy and Lily are twins. Today is their birthday. Mother buys ...

  6. UVA 725 division【暴力枚举】

    [题意]:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出“There are no solut ...

  7. UVA 10603 - Fill BFS~

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

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

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

  9. UVA 10603 Fill

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

随机推荐

  1. 洛谷 P1073 最优贸易 最短路+SPFA算法

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1073 最优贸易 题目描述 C国有 $ n $ 个大城市和 ...

  2. Leetcode720.Longest Word in Dictionary词典中最长的单词

    给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...

  3. 【JZOJ3853】【NOIP2014八校联考第2场第2试9.28】帮助Bsny(help)

    EVRT Bsny的书架乱成一团了,帮他一下吧! 他的书架上一共有n本书,我们定义混乱值是连续相同高度书本的段数.例如,如果书的高度是30,30,31,31,32,那么混乱值为3:30,32,32,3 ...

  4. 遗传算法MATLAB实现(3):多元函数优化举例

    多峰的Shubert为: 求f(x,y)在[-10,10]x[-10,10]上的最大值. MATLAB代码: fun_mutv函数为: function my=fun_mutv(x,y) t1=zer ...

  5. 2017校赛 问题 F: 懒人得多动脑

    题目描述 小D的家A和学校B都恰好在以点F为焦点的双曲线上,而小D每日所需的生活水源在一条平行该双曲线准线的直线上,设它的值为v.大家都知道,每天都是要喝水的,但是小D有点懒,他希望自己能在去上学或者 ...

  6. 【Django入坑之路】Form组件

    1:From组件的简单使用 1创建From: #导入模块 from django import forms from django.forms import fields, widgets # 导入自 ...

  7. hdu2176 尼姆博弈

    如果 a1^a2^a3........^an=0,必败态. 如果 a1^a2^a3........^an!=0,必胜态. 对于必胜态,若a1^a2^a3........^an=k,要让对方为必败态,所 ...

  8. Entity Framework 映射问题

    今天在数据库(mysql)新增了一个字段,但是一直以为添加字段,然后在实体模型中选择 一直是以为选择"添加",就导致有问题,原因就不说,有点蠢,人家都已经存在,还加上去干嘛,我要的 ...

  9. HTTP请求模型

    HTTP请求模型 HTTP请求模型 一.连接至Web服务器一个客户端应用(如Web浏览器)打开到Web服务器的HTTP端口的一个套接字(缺省为80). 例如:http://www.myweb.com: ...

  10. Python操作pymysql写入数据库时的错误

    错误一 InternalError: (pymysql.err.InternalError) (1366, "Incorrect string value: '\\xE6\\xAD\\xA3 ...