There is an interesting calculator. It has 3 rows of button.
? Row 1:button 0, 1, 2, 3, ..., 9. Pressing each button appends this digit to the end of the display.
? Row 2:button +0, +1, +2, +3, ..., +9. Pressing each button adds this digit to the display.
? Row 3:button *0, * *, * *, ..., *9. Pressing each button multiplies this 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 a, you can press button 3, +5, * * to get 256. Similarly, to change the
Display from 0 to 1, you can press 1 or +1 (and not both!).
Each button had a positive cost, your task was to change the display from X to Y with minimum cost.
If There is multiple ways to does so, the number of presses should is minimized.
Input
There'll is at the most test cases. The first line of all test case contains integers x and y
(0≤x≤y≤105
). Each of the 3 lines contains 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
This problem is a SPFA
Template questions
I use two-way wide search did not write out .....
#include <cstdio>#include <cstring>#include <queue>Using namespaceSTD;#include <iostream>/ * NAME:COPYRIGHT:AUTHOR:DATE:07/05/16 13:42 description:*/const INT Maxn=100005;const INT inf=0x3f3f3f3f;struct Note {int num,step,cost;Note () {} note (intxIntyIntZ) {num=x;cost=y;step=Z;}} A[MAXN];Intx[ One];Inty[ One];IntZ[ One];int N,m;int Cost,step;boolinch(int nums) {if (nums>=0&&NUMS<=M) return True;return False;}void BFs () {queue<note> que;while (!que. Empty()) {que. Pop();} for (int i=0; i<maxn; ++i) {A[i]. Step=a[i]. Cost=inf;A[i]. Num=i;} A[n]. Step=0;A[n]. Cost=0;Que. Push(Note (n,0,0));while (!que. Empty()) {struct Note p=que. Front();Que. Pop();for (int i=0; i<10; ++i) {int num=p. Num*Ten+i;Ifinch(num)) {if (A[num]. Cost> (a[p. Num]. Cost+x[i])) {A[num]. Cost=a[p. Num]. Cost+xI;A[num]. Step=a[p. Num]. Step+1;Que. Push(Note (num,a[num). Cost, A[num]. Step));} else if (A[num]. Cost= = (a[p. Num]. Cost+x[i]) && (A[num]. Step>a[p. Num]. Step+1)) {A[num]. Step=a[p. Num]. Step+1;Que. Push(Note (num,a[num). Cost, A[num]. Step));}} num=p. Num+i;Ifinch(num)) {if (A[num]. Cost> (a[p. Num]. Cost+y[i])) {A[num]. Cost=a[p. Num]. Cost+yI;A[num]. Step=a[p. Num]. Step+1;Que. Push(Note (num,a[num). Cost, A[num]. Step));} else if (A[num]. Cost= = (a[p. Num]. Cost+y[i]) && (A[num]. Step>a[p. Num]. Step+1)) {A[num]. Step=a[p. Num]. Step+1;Que. Push(Note (num,a[num). Cost, A[num]. Step));}} num=p. Num*i;Ifinch(num)) {if (A[num]. Cost> (a[p. Num]. Cost+Z[i])) {A[num]. Cost=a[p. Num]. Cost+ZI;A[num]. Step=a[p. Num]. Step+1;Que. Push(Note (num,a[num). Cost, A[num]. Step));} else if (A[num]. Cost= = (a[p. Num]. Cost+Z[i]) && (A[num]. Step>a[p. Num]. Step+1)) {A[num]. Step=a[p. Num]. Step+1;Que. Push(Note (num,a[num). Cost, A[num]. Step));}}}}}int main () {int coun=0;#ifndef Online_judgeFreopen ("IC.txt","R", stdin);Freopen ("I.in","R", stdin);Freopen ("OUT.txt","W", stdout);#endif//Online_judgewhile (scanf ("%d%d", &n,&m)!=-1) {for (int i=0; i<10; ++i) {scanf"%d",&x[i]);} for (int i=0; i<10; ++i) {scanf"%d",&y[i]);} for (int i=0; i<10; ++i) {scanf"%d",&Z[i]);} printf ("Case%d:", ++coun);if (! ( n^m)) {printf ("0 0\n");Continue;} BFS ();printf"%d%d\n", A[m]. Cost, A[m]. Step);} return0;}/**/
Interesting Calculator Hunan Ninth provincial race