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 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
【分析】这跟昨天的 母亲的牛奶那题特别像,两个瓶,一开始都为空,有三种操作,将一个瓶灌满,讲一个瓶清空,然后就是将一个瓶网另一个瓶倒,直到到完
或者到满。将昨天的代码稍微改了一下。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=;
int n,dp[N],len;
int w[N][N];
int g[];
int a,b,c;
int X,Y,K,M;
set<int>p;
int minn=inf;
struct man {
int x,y;
int step;
}; void bfs() {
w[][]=;
queue<man>q;
man s;
s.x=;
s.y=;
s.step=;
q.push(s);
while(!q.empty()) {
man t=q.front();
q.pop();
minn=min(minn,abs((t.x+t.y)-M));
if(t.step==K)continue;
int f[];
f[]=t.x;
f[]=t.y;
if(!w[f[]][]){man kk;kk.x=f[];kk.y=;kk.step=t.step+;w[f[]][]=;q.push(kk);}
if(!w[f[]][g[]]){man kk;kk.x=f[];kk.y=g[];kk.step=t.step+;w[f[]][g[]]=;q.push(kk);}
if(!w[][f[]]){man kk;kk.x=;kk.y=f[];kk.step=t.step+;w[][f[]]=;q.push(kk);}
if(!w[g[]][f[]]){man kk;kk.x=g[];kk.y=f[];kk.step=t.step+;w[g[]][f[]]=;q.push(kk);}
for(int i=; i<; i++) {
f[]=t.x;
f[]=t.y;
if(f[i]==)continue;
for(int j=; j<; j++) {
f[]=t.x;
f[]=t.y;
man k;
if(i==j||f[j]==g[j])continue;
if(f[i]+f[j]>=g[j]) {
f[i]=f[i]-(g[j]-f[j]);
f[j]=g[j];
// printf("@%d %d\n",f[i],f[j]);
} else if(f[i]+f[j]<g[j]) {
f[j]+=f[i];
f[i]=; }
k.x=f[];
k.y=f[];
// printf("!!!%d %d %d\n",k.x,k.y,k.z);
if(w[k.x][k.y]==) {
k.step=t.step+;
q.push(k);
w[k.x][k.y]=;
}
}
}
}
} int main() {
memset(w,,sizeof(w)); cin>>X>>Y>>K>>M;
g[]=X;
g[]=Y;
bfs();
cout<<minn<<endl;
return ;
}

Milk Pails(BFS)的更多相关文章

  1. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  2. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  3. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  4. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  5. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  6. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  7. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  8. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

  9. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

随机推荐

  1. 【题解】APIO2013机器人

    其实这题前前后后的思考时间加起来应该有两天之久了,dp状态,转移方式等等都还是比较好想,然而左看右看觉得spfa复杂度未免太爆炸……然后选择看了一篇题解,发现在多重优化之下,其实是可以过的…… 首先建 ...

  2. 【BZOJ 2553】[BeiJing2011]禁忌 AC自动机+期望概率dp

    我一开始想的是倒着来,发现太屎,后来想到了一种神奇的方法——我们带着一个既有期望又有概率的矩阵,偶数(2*id)代表期望,奇数(2*id+1)代表概率,初始答案矩阵一列,1的位置为1(起点为0),工具 ...

  3. 【BZOJ 3505】 [Cqoi2014]数三角形 容斥原理+排列组合+GCD

    我们先把所有三角形用排列组合算出来,再把一行一列上的三点共线减去,然后我们只观察向右上的三点共线,向左上的乘二即可,我们发现我们如果枚举所有的两边点再乘中间点的个数(GCD),那么我们发现所有的两边点 ...

  4. Cube 找规律

    这道题我们经过简单的推测便可得知3个之前特判,四个之后就成为了一般状况,就是我们每侧都是走整个整个的|_|之后零的走|||. 考试的时候包括平时做题,许多正确的感性比理性证明要强得多. #includ ...

  5. 小K的农场

    小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了 ...

  6. HDU3605:Escape(状态压缩+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  7. URAL - 1486 Equal Squares 二维哈希+二分

    During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...

  8. ubuntu12.04 Qt WebKit编译

    转载自:http://my.oschina.net/u/257674/blog/167050 官方文档: http://trac.webkit.org/wiki/BuildingQtOnLinux#D ...

  9. babel-preset-es2015,babel-polyfill 与 babel-plugin-transform-runtime

    babel-preset-es2015 是一个babel的插件,用于将部分ES6 语法转换为ES5 语法.转换的语法包括: 箭头函数 var a1 = () => 1 编译为 var a1 = ...

  10. 【数据结构】bzoj2957楼房重建

    Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...