Milk Pails

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+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,x,y,k,mi,vis[maxn][maxn];
void bfs()
{
    queue<pii >p;p.push({0,0});vis[0][0]=1;
    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+1)continue;
        if(u<x&&!vis[x][v])p.push({x,v}),vis[x][v]=vis[u][v]+1;
        if(v<y&&!vis[u][y])p.push({u,y}),vis[u][y]=vis[u][v]+1;
        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]+1;
        }
        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]+1;
        }
        if(u&&!vis[0][v])p.push({0,v}),vis[0][v]=vis[u][v]+1;
        if(v&&!vis[u][0])p.push({u,0}),vis[u][0]=vis[u][v]+1;
    }
}
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 0;
}
时间: 2024-07-30 16:21:45

Milk Pails的相关文章

Milk Pails(BFS)

Milk Pails 时间限制: 1 Sec  内存限制: 64 MB提交: 16  解决: 4[提交][状态][讨论版] 题目描述 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 h

USACO 5.3 Milk Measuring

Milk MeasuringHal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest milk and deliver it in one big bottle to a customer. He fills that bottle with exactly the number of quarts that the customer orders. Farmer John has always b

USACO Section 5.3 Milk Measuring (IDDFS+dp)

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

[BZOJ1717][Usaco2006 Dec]Milk Patterns 产奶的模式

1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1297  Solved: 705 [Submit][Status][Discuss] Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". John的牛奶按质量可以被赋予一个0到100

洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk

题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的.此外,就像每头奶牛每天只能挤出固定数量的奶,每位奶农每天能提供的牛奶数量是一定的.每天Marry乳业可以从奶农手中采购到小于或者等于奶农最大产量的整数数量的牛奶. 给出Marry乳业每天对牛奶的需求量,还有每位奶农提供的牛奶单价和产量.计算采购足够数量的牛奶所需的最小花费. 注:每天所有奶农

Mixing Milk

链接 分析:水题,按照价格从小到大排序,在进行贪心即可 /* PROB:milk ID:wanghan LANG:C++ */ #include "iostream" #include "cstdio" #include "cstring" #include "string" #include "algorithm" using namespace std; const int maxn= 5000+10;

洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling

题目描述 Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes only one unit of time to milk. Being impatient animals, some cows will refuse to be milked if Farmer John waits too long to milk them. More specifically, cow

母亲的牛奶(milk) (BFS)

问题 A: 母亲的牛奶(milk) 时间限制: 1 Sec  内存限制: 64 MB提交: 14  解决: 8[提交][状态][讨论版] 题目描述 农民约翰有三个容量分别是A.B.C升的桶,A.B.C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一 个桶倒到另一个桶中,直到被灌桶装满或原桶空了.当然每一次灌注都是完全的,由于节约,牛奶不会丢失.写一个程序去帮助约翰找出当A桶是空的时候,C桶中 牛奶所剩量的所有可能性. 输入 单独的1行,包括三个整数A,

USACO 1.3 Mixing Milk(贪心)

USACO Mixing Milk 简单的贪心,读入数据,按单价从小到大排序,然后从最便宜的买起,直到买够为止. /* ID:twd30651 PROG:milk LANG:C++ */ #include<iostream> #include<fstream> #include<stdlib.h> using namespace std; int N; int M; typedef struct node { int P; int A; }node; node data