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. 理解First Chance和Second Chance避免单步调试

    原文链接地址:http://blog.csdn.net/Donjuan/article/details/3859160 在现在C++.Java..Net代码大行其道的时候,很多代码错误(Bug)都是通 ...

  2. [Leetcode] container with most water 最大水容器

    Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...

  3. git使用笔记(五)打标签

    By francis_hao    Nov 19,2016 当一个项目commit了若干次到了一个可以发布版本的时候一般会给当前的分支状态打一个标签,就像我们常常见到的V1.0之类的. Git 使用的 ...

  4. 2018 BAT最新 php面试必考题

    收集一些实用php面试题及答案给大家 做为程序员,到IT企业面试的时候肯定会有笔试这关,那就要考考你的PHP知识了,所以本站收集一些实用的php面试题及答案给大家.  基础题:  1.表单中 get与 ...

  5. Codeforces Round #524 (Div. 2) B. Margarite and the best present

    B. Margarite and the best present 题目链接:https://codeforces.com/contest/1080/problem/B 题意: 给出一个数列:an=( ...

  6. TCP ------ TCP创建服务器中出现的套接字

    在服务器端,socket()返回的套接字用于监听(listen)和接受(accept)客户端的连接请求.这个套接字不能用于与客户端之间发送和接收数据. accept()接受一个客户端的连接请求,并返回 ...

  7. npm安装node-sass失败,EACCES: permission denied

    增加--unsafe-perm,即 sudo npm install node-sass --unsafe-perm --save-dev 成功安装node-sass

  8. jsonp应用

    1.服务端jsonp格式数据 如客户想访问 : http://www.runoob.com/try/ajax/jsonp.php?jsonp=callbackFunction. 假设客户期望返回JSO ...

  9. ssh中的相对路径与绝对路径的问题

    一:前言:自己在学习ssh的时候常常被路径给迷惑,就比如在刚刚学习jsp的servlet时,绝对路径和相对路径我就弄混了,所以专门写了一篇博客来记载.而现在自己是在学ssh的时候在此遇到路径问题,本来 ...

  10. netty学习指南

    这段时间领导让我熟悉Socket开发,我花了三周时间左右去学习相关的知识,包括Java socket开发,重点学习了netty这个异步非阻塞通信框架. 在这里把我学习过程中遇到的有用资料整理了,供大家 ...