Description
There is an interesting calculator. It has 3 rows of buttons.
Row 1: button 0, 1, 2, 3,..., 9. Pressing each buttonAppendsThat digit to the end of the display.
Row 2: button + 0, + 1, + 2, + 3,..., + 9. Pressing each buttonAddsThat digit to the display.
Row 3: button * 0, * 1, * 2, * 3,..., * 9. Pressing each buttonMultipliesThat 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 shocould 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 2561 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 112 256100 100 100 1 100 100 100 100 100 100100 100 100 100 100 1 100 100 100 100100 100 10 100 100 100 100 100 100 100
Sample output
Case 1: 2 2 Case 2: 12 3To change the first number to the second number, add 0 to the end of the previous number ~ 9 Add 0 to the previous number ~ 9. The previous number is multiplied by 0 ~ 9 each operation requires a certain amount of cost. The minimum cost for the second operation is calculated in the table and the number of its operations. This question is about the master of the priority queue...Code:# Include <iostream> # include <cstdio> # include <cstring> # include <queue> using namespace STD; struct node {int X, num, sum; friend bool operator <(const node AA, node bb) {If (AA. sum! = BB. sum) return AA. sum> BB. SUM; // else return AA. num> BB. num; // The cost is the same as the number of times.}; int vis [100005]; // indicates whether the number has exceeded int Q [4] [20]; int A, B; int ans, s; void BFS () {memset (VIS, 0, sizeof (VIS); int I; priority_queue <node> QQ; node now, next; now. num = 0; now. sum = 0; now. X = A; QQ. push (now); While (! QQ. empty () {now = QQ. top (); QQ. pop (); If (vis [now. x]) // The Key To this question. vis [now. x] = 1; // The Key to this question is to use the priority queue to mark the state with the smallest current number of charges to filter the State where the cost is greater than the same if (now. X = B) {ans = now. SUM; S = now. num; return ;}for (I = 0; I <= 9; I ++) {next. X = now. x * 10 + I; next. sum = now. sum + Q [1] [I]; If (next. x <= B &&! Vis [next. x]) {// The vis [] array marked in the status cannot appear here .. Otherwise, it is not the minimum cost .. Next. num = now. num + 1; QQ. push (next);} next. X = now. X + I; next. sum = now. sum + Q [2] [I]; If (next. x <= B &&! Vis [next. x]) {next. num = now. num + 1; QQ. push (next);} next. X = now. x * I; next. sum = now. sum + Q [3] [I]; If (next. x <= B &&! Vis [next. x]) {next. num = now. num + 1; QQ. push (next) ;}}} int main () {int I, j; int T = 0; while (~ Scanf ("% d", & A, & B) {T ++; for (I = 1; I <= 3; I ++) for (j = 0; j <= 9; j ++) scanf ("% d", & Q [I] [J]); BFS (); printf ("case % d: % d \ n", T, ANS, S);} return 0 ;}
Csuoj 1336 interesting calculator priority queue