Milk Pails
Milk Pails
题目描述
- He can fill either pail completely to the top.
- He can empty either pail.
- He can pour the contents of one pail into the other, stopping when the former becomes empty or the latter becomes full (whichever of these happens first).
Although FJ realizes he may not be able to end up with exactly M total units of milk in the two pails, please help him compute the minimum amount of error between M and the total amount of milk in the two pails. That is, please compute the minimum value of |M−M′| such that FJ can construct M′ units of milk collectively between the two pails.
输入
输出
样例输入
14 50 2 32
样例输出
18
分析:枚举所有可能情况,bfs即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e3+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,x,y,k,mi,vis[maxn][maxn];
void bfs()
{
queue<pii >p;p.push({,});vis[][]=;
while(!p.empty())
{
int u=p.front().fi,v=p.front().se;
p.pop();
mi=min(mi,abs(u+v-m));
if(vis[u][v]==k+)continue;
if(u<x&&!vis[x][v])p.push({x,v}),vis[x][v]=vis[u][v]+;
if(v<y&&!vis[u][y])p.push({u,y}),vis[u][y]=vis[u][v]+;
if(u<x&&v)
{
int mi=min(x-u,v);
if(!vis[u+mi][v-mi])p.push({u+mi,v-mi}),vis[u+mi][v-mi]=vis[u][v]+;
}
if(v<y&&u)
{
int mi=min(y-v,u);
if(!vis[u-mi][v+mi])p.push({u-mi,v+mi}),vis[u-mi][v+mi]=vis[u][v]+;
}
if(u&&!vis[][v])p.push({,v}),vis[][v]=vis[u][v]+;
if(v&&!vis[u][])p.push({u,}),vis[u][]=vis[u][v]+;
}
}
int main()
{
int i,j,t;
scanf("%d%d%d%d",&x,&y,&k,&m);
mi=m;
bfs();
printf("%d\n",mi);
//system ("pause");
return ;
}
Milk Pails的更多相关文章
- Milk Pails(BFS)
Milk Pails 时间限制: 1 Sec 内存限制: 64 MB提交: 16 解决: 4[提交][状态][讨论版] 题目描述 Farmer John has received an order ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- USACO Section 5.3 Milk Measuring (IDDFS+dp)
迭代加深搜索,从小到大枚举桶数的上限maxd:对每个maxd,枚举每个组合,判断是否能够倒出q:直到得到answer.判断的部分就用dp(完全背包). ------------------------ ...
- USACO 5.3 Milk Measuring
Milk MeasuringHal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest mil ...
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1017 Solved: ...
- POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Milk Patterns Description Farmer John has noticed that the quality of milk given by his cows varie ...
- 【BZOJ-1717】Milk Patterns产奶的模式 后缀数组
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 881 Solved: ...
- 后缀数组---Milk Patterns
POJ 3261 Description Farmer John has noticed that the quality of milk given by his cows varies from ...
- Milk
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) ...
随机推荐
- Xcode6无法用xib得问题解决方法
1.创建一个新工程,选择singleView application 2.将storyboard和launchscreen删除,选择moveToTrash 3.删除info.plist文件中Main ...
- jsp 页面取值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- UIView的基本属性及ANimation
frame属性:可以使用该属性改变尺寸和位置 相对于父视图bounds:改变尺寸 相对自身center:改变视图的位置alpha:改变视图的透明度backgroundColor:改变视图的背景cont ...
- seo优化 标点符号
一.顿号“.” 顿号是一个只有在中文中使用的标点符号,这在英文中没有.毕竟该不该在标题中使用顿号呢,建议大家仍是不要使用,或者说在标题中就不要泛起中文的符号最好.不外,顿号可以使用在Descripti ...
- Spring Boot 系列教程10-freemarker导出word下载
freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个 ...
- linux 下载安装tomcat
1.下载tomcat 2.tar -zxvf apache-tomcat-7.0.67.tar.gz 3.cd bin目录 启动 ./startup.sh [root@fuyuanming bin ...
- JSP 基础
定义 JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写html,但它相 ...
- java 类与对象
class XiyoujiRenwu { float height,weight; String head, ear; void speak(String s) { System.out.printl ...
- 上传Android或Java库到Maven central repository(转载)
主要介绍利用Sonatype将jar或aar提交到Maven的中央仓库. 是不是希望将自己的jar或是aar传到maven官方库中,在The Central Repository中可以被其他人搜索使用 ...
- spring项目中的定时任务实现和问题解决
之前我用JAVA中的Timer类实现了服务器的定时任务,具体详见之前的博文. 后来发现了一个更简单的实现方式,利用spring中的@Scheduled注解实现觉得简单的很多. 确实spring封装的特 ...