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. [POI2011]Lightning Conductor

    题面在这里 description 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\). 对于每个\(1\le i\le n\),找到最小的非负整数\(p\), 满足对于任意的\(1 ...

  2. vector 进阶

    http://classfoo.com/ccby/article/jnevK #include <iostream> #include <vector> #include &l ...

  3. Intellij Idea debug 远程部署的的tomcat项目

    web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是Intellij idea,可以通过如下方法实现: 开启debug端口,启动tomcat 以tomcat7.0.75为例,打开bi ...

  4. jsonp应用

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

  5. 数据结构基础---Binary Search Tree

    /// Binary Search Tree - Implemenation in C++ /// Simple program to create a BST of integers and sea ...

  6. python并发进程

    1 引言 2 创建进程 2.1 通过定义函数的方式创建进程 2.2 通过定义类的方式创建进程 3 Process中常用属性和方法 3.1 守护进程:daemon 3.2 进程终结于存活检查:termi ...

  7. 解决mysql报错:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'

    mysql执行报错: - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...

  8. 【洛谷 P3834】 可持久化线段树1(主席树)

    题目链接 主席树=可持久化权值线段树. 如果你不会可持久化线段树,请右转 如果你不会权值线段树,请自行脑补,就是线段树维护值域里有多少个数出现. 可持久化线段树是支持查询历史版本的. 我们对每个数都进 ...

  9. [bzoj4765]普通计算姬——分块

    Brief Description 给定一棵n个节点的带权树,节点编号为1到n,以root为根,设sum[p]表示以点p为根的这棵子树中所有节点的权 值和.支持下列两种操作: 1 给定两个整数u,v, ...

  10. [bzoj3524==bzoj2223][Poi2014]Couriers/[Coci 2009]PATULJCI——主席树+权值线段树

    题目大意 给定一个大小为n,每个数的大小均在[1,c]之间的数列,你需要回答m个询问,其中第i个询问形如\((l_i, r_i)\),你需要回答是否存在一个数使得它在区间\([l_i,r_i]\)中出 ...