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.
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.
For each test case, print the minimal cost and the number of presses.
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
Case 1: 2 2Case 2: 12 3
思路:起点为x,终点为y 。。。。 其他的都当做是路,然后每次取出来最小的花费即可!!!
当然,这题也可以用刷表法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
int x,y;
struct P
{
int v,c,step;
P(int v,int c,int step):v(v),c(c),step(step){}
P(){}
bool operator < (const P& t1)const
{
if(c==t1.c) return step>t1.step;
return c>t1.c;
}
};
int dp[N],step[N];
int a[4][11];
void dij()
{
int i,j,num,cost;
priority_queue<P>que;
fill(dp,dp+y+1,inf);
fill(step,step+y+1,inf);
dp[x]=0,step[x]=0;
que.push(P(x,0,0));
while(!que.empty()) {
P now = que.top();
que.pop();
if(dp[now.v]<now.c) continue;
else if(dp[now.v]==now.c && step[now.v]<now.step) continue;
for(i=0;i<10;i++) {
num = now.v*10+i;
if(num>y) continue;
if(dp[num]>dp[now.v]+a[1][i]) {
dp[num]=dp[now.v]+a[1][i];
step[num]=step[now.v]+1;
que.push(P(num,dp[num],step[num]));
}
}
for(i=0;i<10;i++) {
num = now.v+i;
if(num>y) continue;
if(dp[num]>dp[now.v]+a[2][i]) {
dp[num]=dp[now.v]+a[2][i];
step[num]=step[now.v]+1;
que.push(P(num,dp[num],step[num]));
}
}
for(i=0;i<10;i++) {
num = now.v*i;
if(num>y) continue;
if(dp[num]>dp[now.v]+a[3][i]) {
dp[num]=dp[now.v]+a[3][i];
step[num]=step[now.v]+1;
que.push(P(num,dp[num],step[num]));
}
}
}
}
int main()
{
int i,j,cc=0;
while(scanf("%d%d",&x,&y)!=EOF) {
for(i=1;i<=3;i++)
for(j=0;j<10;j++)
scanf("%d",&a[i][j]);
dij();
printf("Case %d: %d %d\n",++cc,dp[y],step[y]);
}
return 0;
}