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. [PHPCMS V9二次开发]自定义字段模型-添加字段类型

    步骤/方法 打开phpcms\modules\content\fields目录,复制文件夹downfiles,并改名为textgroups. 打开phpcms\modules\content\fiel ...

  2. Linux与Unix shell编程指南(完整高清版).pdf

    找到一本很详细的Linux Shell脚本教程,其实里面不光讲了Shell脚本编程,还介绍了系统的各种命令 http://vdisk.weibo.com/s/yVBlEojGMQMpv 本书共分五部分 ...

  3. Nginx教程(五) Nginx配置文件详解 (转)

    一. Nginx配置文件nginx.conf中文详解 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processe ...

  4. poj2987 最大权闭合图

    基础题. 最小割后,与汇点相连的点都不要,然后从源点出发dfs一遍有多少相连的点即可. #include<stdio.h> #include<string.h> #includ ...

  5. 如何用好消息推送(push)做APP运营

    作为移动端APP产品运营最重要的运营手段,消息推送(push)被越来越多的APP厂商所重视,在信息泛滥的移动互联网时代,手机APP应用安装得越来越多,小小的手机屏幕每天收到的消息推送也越来越多,站在用 ...

  6. python 面向对象编程语言

  7. python 只导入某个对象

  8. SDUT-2140_判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个有向图,判 ...

  9. 如何在Liferay 7中创建一个简单的JSF Portlet

    这个将在Liferay IDE 3.1 M3的发布版中提供创建的选项,但是你也可以通过命令行来创建. 1.这是Liferay JSF团队的官网:http://liferayfaces.org/ 你能在 ...

  10. PLAY2.6-SCALA(七) Streaming HTTP response

    1.从HTTP1.1开始,服务端为了在single connection下对HTTP请求及响应提供服务,需要在response中提供响应的Content-Length. 默认情况下,不需要显示的指明C ...