D - Interesting Calculator 【数值型BFS+优先队列】
There is an interesting calculator. It has 3 rows of buttons.
Row 1: button 0, 1, 2, 3, ..., 9. Pressing each button appends that digit to the end of the display.
Row 2: button +0, +1, +2, +3, ..., +9. Pressing each button adds that digit to the display.
Row 3: button *0, *1, *2, *3, ..., *9. Pressing each button multiplies that digit to the display.
Note that it never displays leading zeros, so if the current display is 0, pressing 5 makes it 5 instead of 05. If the current display is 12, you can press button 3, +5, *2 to get 256. Similarly, to change the display from 0 to 1, you can press 1 or +1 (but not both!).
Each button has a positive cost, your task is to change the display from x to y with minimum cost. If there are multiple ways to do so, the number of presses should be minimized.
Input
There will be at most 30 test cases. The first line of each test case contains two integers x and y(0<=x<=y<=105). Each of the 3 lines contains 10 positive integers (not greater than 105), i.e. the costs of each button.
Output
For each test case, print the minimal cost and the number of presses.
Sample Input
12 256
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
12 256
100 100 100 1 100 100 100 100 100 100
100 100 100 100 100 1 100 100 100 100
100 100 10 100 100 100 100 100 100 100
Sample Output
Case 1: 2 2
Case 2: 12 3
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std;
struct Node
{
int time;
int cost;
int value;
friend bool operator < (Node a,Node b)
{
if(a.cost==b.cost)
return a.time>b.time;
return a.cost>b.cost;
}
}node;
priority_queue<Node> q;
int val[];
int mp[][];
int cas=;
int x,y,i,j; void bfs()
{
node.time=;
node.cost=;
node.value=x;
while(!q.empty()) q.pop();
q.push(node);
val[x]=;
while(!q.empty())
{
Node tp,tmp=q.top();
q.pop();
if(tmp.value==y)
{
printf("Case %d: %d %d\n",cas++,tmp.cost,tmp.time);
return ;
}
for(int i=;i<;i++)
{
for(int j=;j<=;j++)
{
if(i==)
tp.value=tmp.value*+j;
else if(i==)
tp.value=tmp.value+j;
else
tp.value=tmp.value*j; tp.cost=tmp.cost+mp[i][j];
tp.time=tmp.time+; if(tp.value<=y&&tp.cost<val[tp.value])
{
q.push(tp);
val[tp.value]=tp.cost;
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&x,&y))
{
for(int i=;i<;i++)
val[i]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
scanf("%d",&mp[i][j]);
}
}
bfs();
}
return ;
}
D - Interesting Calculator 【数值型BFS+优先队列】的更多相关文章
- G - Rescue 【地图型BFS+优先队列(有障碍物)】
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- E - A strange lift 【数值型BFS+上下方向】
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...
- C - 你经历过绝望吗?两次! 【地图型BFS+优先队列(障碍物)】
4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的“猪坚强”.不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚 ...
- 中南大学oj:1336: Interesting Calculator(广搜经典题目)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...
- CSU-1336: Interesting Calculator,最短路思想!
1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...
- 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator
Interesting Calculator Time Limit: 2 Sec Memory Limit: 128 MB Submit: 163 Solved: 49 Description T ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- heap表按字符串和数值型排序规则
SQL> create user scan identified by scan default tablespace users; User created. SQL> grant db ...
- Swift编程语言学习1.4——数值型字面量、数值类型转换
数值型字面量 整数字面量能够被写作: 一个十进制数,没有前缀 一个二进制数,前缀是0b 一个八进制数,前缀是0o 一个十六进制数,前缀是0x 以下的全部整数字面量的十进制值都是17: let deci ...
随机推荐
- 【bzoj2957】楼房重建 分块+二分查找
题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子.为了简化问题,我们考虑这些事件发生在一个二 ...
- 【题解】洛谷P1975排序
分块,注意重复的值之间的处理.跟普通分块的操作一样的啦,具体可以参见‘不勤劳的图书管理员’. #include <bits/stdc++.h> using namespace std; # ...
- SCOI 股票交易 单调队列优化dp
这道题 我很蒙.....首先依照搞单调队列优化dp的一般思路 先写出状态转移方程 在想法子去优化 这个题目中说道w就是这一天要是进行操作就是从前w-1天转移而来因为之前的w天不允许有操作!就是与这些天 ...
- JavaScript渐变效果的实现
鼠标移上去透明度渐渐增加,鼠标移出,透明度渐渐减小. 关键代码: view source print? 1 var speed = 0; 2 if(target>obj.alpha){ 3 ...
- 前端部署: nginx配置
前提:nginx 已安装 简介:nginx(engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Ram ...
- Struts2 利用拦截器 interceptor 控制登陆和访问权限
最近学习了Struts2的登录和权限控制用到的是拦截器,需要在struts.xml中配置,每个action都默认的继承defaultStack,如果你用了别的拦截器,还需要手动引入defaultSta ...
- java.sql.Date和java.util.Date的不同和相互转换方式
一:前言 这是我在新的公司写的第一份博客吧,来了又一个星期了吧,但是在来的那几天我真的很迷茫的感觉这里是很不适合我的样子,而且我又是来实习的,我很不愿意啊,自己做的又是java web,最原始的ser ...
- Spring 学习笔记(二)
一.Spring 中的bean配置 –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).Fac ...
- [bzoj3238][Ahoi2013]差异——后缀自动机
Brief Description Algorithm Design 下面给出后缀自动机的一个性质: 两个子串的最长公共后缀,位于这两个串对应的状态在parent树上的lca状态上.并且最长公共后缀的 ...
- WC后记
这次去WC本来就是抱着玩儿玩儿的心态去的,结果真算是玩儿了... 我们去的内天北京正好下雪,结果后来等我舅接我们去八十中的时候还在外面等了半个小时,其实雪天在外面挺好的,除了旁边都是一些男程序员.后来 ...