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. QML插件扩展(一)

    准备分两节来介绍QML扩展插件,分别为 (一)基于QML文件的扩展方式 (二)基于C++的插件扩展 这篇先介绍基于QML的插件扩展. 先介绍几个基本概念: qmldir: 用于组织自定义的QML插件, ...

  2. nginx libpcre.so.1: cannot open shared object file

    linux 64位安装nginx后启动出错报以下错误 1 2 3 [root@localhost nginx-1.3.0]# /usr/local/nginx/sbin/nginx error whi ...

  3. redis采用序列化方案存对象

    前几天被问到这样一个问题,redis怎么存对象,平时也没怎么注意,只知道redis存字符之类的,不过就是根据键存取值,不过对象的话还是不同的 首先来了解一下为什么要实现序列化 为什么要实现序列化接口 ...

  4. 【单源最短路】dijstra poj 1502

    #include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...

  5. FZU 2113 BCD Code 数位dp

    数位dp,但是很奇怪的是我在虚拟oj上用GUC C++提交会wa,用Visual c++提交正确,但是加上注释后提交又莫名CE--好任性啊 0 ,0 题目思路:看代码吧 注释很详细 #include& ...

  6. less分页阅读

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  7. CI 路由设置

    $route['news/show/(:num)']='news/show/$1';   前边是匹配 网址   后边是  指定控制器 在application/config/文件夹下有routes.p ...

  8. 转载 Deep learning:六(regularized logistic回归练习)

    前言: 在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在lo ...

  9. 常用的JS页面跳转代码调用大全

    一.常规的JS页面跳转代码 1.在原来的窗体中直接跳转用 <script type="text/javascript"> window.location.href=&q ...

  10. World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)

    分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代 ...