UVa - 12664 - Interesting Calculator

先上题目:

12664 Interesting Calculator
There is an interesting calculator. It has 3 rows of button.
• 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

 

  题意:给出一个数x和一个数y,有30种操作,每种操作需要付出一定的代价,问你从x变成y最少需要多少代价。

  DP,想法是从小到大枚举每一个可能变成的数,然后枚举不同操作会变成的值,保存变成这个值的最小代价以及步数,最终推导到目标数就可以了。

  这一题需要注意的东西是:①在最小代价的基础上还需要最少步数,②有可能先变成0再变成其他数的路径所需要的代价更短。

  所以我们一开始可以初始化dp[0]=乘上0的代价,同时把步数变成一,然后才初始化dp[x]=0,步数为0,然后从小到大扫描就可以了。

 

上代码:

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define MAX 100002
 5 #define ll long long
 6 #define INF (((ll)1)<<60)
 7 using namespace std;
 8 
 9 ll dp[MAX],e;
10 int cnt[MAX];
11 int x,y;
12 
13 ll cost[3][10];
14 
15 int main()
16 {
17     int t;
18     //freopen("data.txt","r",stdin);
19     t=1;
20     while(scanf("%d %d",&x,&y)!=EOF){
21         for(int i=0;i<3;i++)   for(int j=0;j<10;j++)   scanf("%lld",&cost[i][j]);
22         for(int i=0;i<=y;i++){
23             dp[i]=INF;  cnt[i]=0;
24         }
25         if(x!=0){dp[0]=cost[2][0];  cnt[0]=1;}
26         dp[x]=0;
27         for(int i=0;i<=y;i++){
28             for(int j=0;j<10;j++){
29                 e=i*10+j;
30                 if(e<=y){
31                     ll u=dp[i]+cost[0][j];
32                     if(dp[e]>u){
33                         dp[e]=u;
34                         cnt[e]=cnt[i]+1;
35                     }else if(dp[e]==u && cnt[e]>cnt[i]+1){
36                         cnt[e]=cnt[i]+1;
37                     }
38                 }
39             }
40             for(int j=0;j<10;j++){
41                 e=i+j;
42                 if(e<=y){
43                     ll u=dp[i]+cost[1][j];
44                     if(dp[e]>u){
45                         dp[e]=u;
46                         cnt[e]=cnt[i]+1;
47                     }else if(dp[e]==u && cnt[e]>cnt[i]+1){
48                         cnt[e]=cnt[i]+1;
49                     }
50                 }
51             }
52             for(int j=0;j<10;j++){
53                 e=(ll)i*j;
54                 if(e<=y){
55                     ll u=dp[i]+cost[2][j];
56                     if(dp[e]>u){
57                         dp[e]=u;
58                         cnt[e]=cnt[i]+1;
59                     }else if(dp[e]==u && cnt[e]>cnt[i]+1){
60                         cnt[e]=cnt[i]+1;
61                     }
62                 }
63             }
64         }
65         printf("Case %d: %lld %d\n",t++,dp[y],cnt[y]);
66 
67     }
68     return 0;
69 }
/*12664*/

 

posted @ 2014-08-13 19:57  海拉鲁的林克  阅读(189)  评论(0编辑  收藏  举报