Milk Pails

题目描述

Farmer John has received an order for exactly M units of milk (1≤M≤200) that he needs to fill right away. Unfortunately, his fancy milking machine has just become broken, and all he has are two milk pails of integer sizes X and Y (1≤X,Y≤100) with which he can measure milk. Both pails are initially empty. Using these two pails, he can perform up to K of the following types of operations (1≤K≤100):

- 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.

输入

The first, and only line of input, contains X, Y, K, and M.

输出

Output the smallest distance from M to an amount of milk FJ can produce.

样例输入

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的更多相关文章

  1. Milk Pails(BFS)

    Milk Pails 时间限制: 1 Sec  内存限制: 64 MB提交: 16  解决: 4[提交][状态][讨论版] 题目描述 Farmer John has received an order ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. USACO Section 5.3 Milk Measuring (IDDFS+dp)

    迭代加深搜索,从小到大枚举桶数的上限maxd:对每个maxd,枚举每个组合,判断是否能够倒出q:直到得到answer.判断的部分就用dp(完全背包). ------------------------ ...

  4. USACO 5.3 Milk Measuring

    Milk MeasuringHal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest mil ...

  5. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

  6. POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

    Milk Patterns   Description Farmer John has noticed that the quality of milk given by his cows varie ...

  7. 【BZOJ-1717】Milk Patterns产奶的模式 后缀数组

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 881  Solved:  ...

  8. 后缀数组---Milk Patterns

    POJ  3261 Description Farmer John has noticed that the quality of milk given by his cows varies from ...

  9. Milk

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) ...

随机推荐

  1. windows7安装oracle 10g

    1.出现如下错误 解决办法: ①确保你有该文件夹的完全控制权.文件夹点右键->属性->安全->高级->所有者->改为自己->编辑自己的权限为完全控制. ②将setu ...

  2. liunx 内存文件 tmpfs

    tmpfs是Linux/Unix系统上的一种基于内存的文件系统.tmpfs可以使用您的内存或swap分区来存储文件 1 创建将被挂载的文件 mkdir /tmp/tmpfs/ 2 写入测试文件内容.大 ...

  3. Thinkphp中使用Redis

    先确保安装了redis扩展 添加Redis配置 'REDIS_HOST'=>'192.168.0.2', 'REDIS_PORT'=>6379, 其他配置根据自己ThinkPHP版本,找到 ...

  4. Inno Setup入门(十七)——Inno Setup类参考(3)

    分类: Install Setup 2013-02-02 11:28 433人阅读 评论(0) 收藏 举报 标签 标签(Label)是用来显示文本的主要组件之一,也是窗口应用程序中最常用的组件之一,通 ...

  5. python 多线程 paramiko实现批量命令输入输出

    远程批量执行命令 实现多线程执行 速度快 实现多并发登录 #-*- coding: utf-8 -*- #!/usr/bin/python import paramiko import threadi ...

  6. TOMCAT-publishing to tomcat v7.0 server at

    因为tomcat的work文件没有清空,导致MyEclipse部署在server.xml文件中的项目路径是错误的. 解决办法:清空work文件夹 下面这个勾勾是MyEclipse自动发布项目路径的选项

  7. 使用oracle数据库开发,异常总结

    最近两天使用的oracle数据库开发项目时遇到了2个异常,第一个是执行sql语句时报异常:“ORA-00911: 无效字符”,如下图: sql语句如下: 断点调试,把sql语句拷贝到pl/sql里执行 ...

  8. js.map error

    1. 问题:      1.1 通过bower install 的components 许多在运行的时候报404无法找到js.map文件, 如图:          2. 分析:     2.1 查看 ...

  9. php中获取各种路径

    echo $_SERVER['DOCUMENT_ROOT'].""; //获得服务器文档根 echo $_SERVER['PHP_SELF'].""; //获得 ...

  10. HDOJ3743<分治>

    题意:求一个排列的逆序数. #include<cstdio> #include<iostream> #include<algorithm> const int ma ...