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 ...
 
随机推荐
- [ZJOI2006]物流运输 DP 最短路
			
---题面--- 题解: 设f[i]表示到第i天的代价,cost[i][j]表示第i天到第j天采取同一种方案的最小代价.那么转移就很明显了,直接$n^2$枚举即可. 所以问题就变成了怎么获取cost数 ...
 - VK Cup 2016 - Round 1 (Div. 2 Edition) A Bear and Reverse Radewoosh
			
A. Bear and Reverse Radewoosh time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
 - jquery 的相关 width 和 height 方法辨析
			
width() 设置或返回元素的宽度(不包括内边距.边框或外边距). height() 设置或返回元素的高度(不包括内边距.边框或外边距). innerWidth() 返回元素的宽度(包括内边距). ...
 - 关于MyBatis的collection集合中只能取到一条数据的问题
			
问题:在涉及多表查询的时候,使用collection元素来映射集合属性时,出现了只能查询到一条数据的情况,但用sql语句在数据库中查询会有多条记录. 解决:如果两表联查,主表和明细表的主键都是id的话 ...
 - Red-Black Tree
			
A red-black tree is a Binary Search Tree that satisfy the red-black tree properties: 1. Every node i ...
 - Sublime Text 3 遇到的一些小坑的解决方法
			
1.[不停弹出更新框]Sublime Text 3 软件会弹出“Update Available”对话框,点击“Cancel”按钮取消:取消之后还是会频繁出现 解决方法:点击菜单栏“Preferenc ...
 - java List排序  顺序  倒序  随机
			
List list = new LinkedList(); for ( int i = 0 ; i < 9 ; i ++ ) { list.add( " a " + i); ...
 - Java内存区域与内存异常
			
参考:深入理解Java虚拟机 周志明 方法区 虚拟机战 本地方法栈 堆 程序计数器 其他 设置 方法区 线程共享,加载类信息.常量.静态变量.JIT后的代码,别名Non-Heap 对于HotSpot, ...
 - (转)Git冲突:commit your changes or stash them before you can merge. 解决办法
			
用git pull来更新代码的时候,遇到了下面的问题: error: Your local changes to the following files would be overwritten by ...
 - cpu_relax( )-----对自选循环等待(spin-wait loops)操作的优化【转】
			
cpu_relax()-----对自选循环等待(spin-wait loops)操作的优化 转自:http://www.doc100.net/bugs/t/173547/index.html 在loc ...