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
题意: 题目的意思是倒水,给出的四个数据是第一个水杯,第二个水杯,第三个水杯,和目标水量.一开始只有第三个水杯是满的,剩下的水杯是空的.倒水的时候只能把倒水出来的这个杯子倒空,或是倒水进去的杯子倒满. ...
随机推荐
- 如何把一个普通的Eclipse项目改造成Eclipse Plugin项目
New Project->Plug-in from existing JAR Archive 同时要记得不仅要将你要转换的项目的jar包选上,同时还要将项目依赖的jar包全部选上(要不然会找不到 ...
- Nginx教程(三) Nginx日志管理 (转)
Nginx教程(三) Nginx日志管理 1 日志管理 1.1 Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某 ...
- No.2 Verilog 模块和描述风格
2-1 模块 Verilog语言基本的描述单元----模块,模块是用来描述某个设计的功能或结构,以及它与其它外部模块进行通信的端口. module module_name(port_list); De ...
- spring boot + mybatis 访问 neo4j
之前有通过rest的风格去访问,但是每次需要访问时候将statement一并加入header中去数据库执行,方式简单.且思路清晰,但是不便于形成模板调用,固采用mybaits来集成. 1.关键pom. ...
- react-cnode
感谢无私开源的程序员们~~~代码因为你们更加美腻~ //根index.js import React, { Fragment } from 'react'; import ReactDOM from ...
- 跟我一起认识axure(一)
第一步下载:https://www.axure.com.cn/ 第二步点击安装,一路next 第三步:认识Axure RP工作界面
- 2016中国银行Top100榜单发布 工行排首位
2016中国银行Top100榜单发布 工行排首位 2016-07-09 15:13:19 第一财经 2016年7月8日,中国银行业协会在京召开“<中国银行业发展报告(2016)>发布会 ...
- hdu 2844 混合背包【背包dp】
http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意:有n种纸币面额(a1,a2,...an),每种面额对应有(c1,c2,...cn)张.问这些钱能拼成 ...
- BZOJ 1008 越狱题解
其实这题很水,显然n个房间有m种宗教,总共有n^m种情况, 我们再考虑不合法的情况,显然第一个房间有m种情况,而后一种只有m-1种情况(因为不能相同) 所以不合法的情况有(m-1)^(n-1)*m种情 ...
- @codeforces - 444A@ DZY Loves Physics
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的图,边有边权,点有点权. 找到一个连通 ...