CSUOJ 1336 Interesting Calculator spfa / 优先队列

Description

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


一年后重写 贴个又快又容易理解的spfa代码, 其实关于优先队列的题,如果能清楚点和边的关系,那么spfa都能写
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 100050
#define INF 0x3f3f3f3f
using namespace std;
int cost[3][10];
int dis[maxn],num[maxn];
int vis[maxn];
int st,ed;
int Case=1;
void spfa()
{
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    queue<int>q;
    int u,v,t;
    q.push(st);
    num[st]=dis[st]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=0; i<3; i++)
            for(int j=0; j<10; j++)
            {
                if(i==0)
                    v=u*10+j;
                else if(i==1)
                    v=u+j;
                else
                    v=u*j;
                if(v>ed)                //注意是break  +1 *1  *10+1  都爆了 就不用考虑后面那些了
                    break;
                if(dis[v]<=dis[u]+cost[i][j])
                    continue;
                dis[v]=dis[u]+cost[i][j];
                num[v]=num[u]+1;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
    }
    printf("Case %d: %d %d\n",Case++,dis[ed],num[ed]);
}
int main()
{
    while(~scanf("%d%d",&st,&ed))
    {
        for(int i=0; i<3; i++)
            for(int j=0; j<10; j++)
                scanf("%d",&cost[i][j]);
        spfa();
    }
    return 0;
}







 题意:为了使 第一个数变成 第二个数 共有30种操作 在前一个数末尾加 0~9 前一个数加上 0~9 前一个数乘 0~9每种操作都需要一定的费用 以表给出求变成第二个的最小费用和它的操作次数这道题考的是对优先队列的掌握。。。 代码:
#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; //费用相同 次数少优先
    }
};
int vis[100005];  //标记这个数是否出现过
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])   //  此题的关键
        continue;
       vis[now.x]=1;    //  此题的关键    利用优先队列  标记当前这个数 费用最小的状态  过滤那些 费用大于它的同一状态
       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])
           {
                                        //标记状态的VIS[]数组不能出现在这里。。 否则就不是最小费用了。。
           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%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 %d\n",t,ans,s);
    }

    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值