Milk Pails(BFS)
Milk Pails
时间限制: 1 Sec 内存限制: 64 MB
提交: 16 解决: 4
[提交][状态][讨论版]
题目描述
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.
输入
输出
样例输入
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)的更多相关文章
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- 【题解】SDOI2009学校食堂
不知道有没有人跟我有一样的感觉……实际上很多的状压DP都不难,然而调到心碎……这题题面看起来很长,还有混合的‘位运算’来吓唬人(实际上就是异或而已).但实际上只要仔细阅读,发现也是一道水水的裸题. 首 ...
- MySQL事物机制具备四点:简称ACID操作
MySQL事物机制具备四点:简称ACID操作 1.原子性:要么都做,要么都不做(两条数据(写入和存储)一步未成功,整体回滚) 2.一致性:数据库的状态改变(两条数据(写入和存储)均成功,符合原子性,但 ...
- clique 解题报告
clique 题目描述 数轴上有 \(n\) 个点,第 \(i\) 个点的坐标为 \(x_i\),权值为 \(w_i\).两个点 \(i\),\(j\) 之间存在一条边当且仅当 \(abs(x_i-x ...
- 适用于iview的表格转Excel插件
在网上找的一个表格转excel插件,经过修改后使其适用于iview中的table组件 let idTmr; const getExplorer = () => { let explorer = ...
- C语言指针大杂烩
By francis_hao Oct 31,2016 指针数组和数组指针 指针数组本身是个数组,数组的内容是指针.形如char *pa[].由于[]优先级高于*,pa先于[]结合表示pa是一个数组,p ...
- 注意@Bean中的initMethod和destroyMethod
@Configuration public class AppConfig { @Bean(initMethod = "init") public Foo foo() { retu ...
- 用静态工厂的方法实例化bean
//代码如下: package com.timo.domain; public class ClientService { //use static factory method create bea ...
- commons
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <g ...
- POJ 1064---二分搜索法
///2.假定一个解并判断是否可行 ///POJ1064 /** Q:有N条绳子,长度分别为Li,从中切割出k条长度相同的绳子, 这K条绳子最长能有多长?保留两位小数 A: 二分搜索模型. 条件C(x ...
- 【Atcoder】ARC 080 E - Young Maids
[算法]数学+堆 [题意]给定n个数的排列,每次操作可以取两个数按序排在新序列的头部,求最小字典序. [题解] 转化为每次找字典序最小的两个数按序排在尾部,则p1和p2的每次选择都必须满足:p1在当前 ...