码迷,mamicode.com
首页 > 其他好文 > 详细

Interesting Calculator 湖南第九届省赛

时间:2016-05-13 01:40:51      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

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

这道题是一个spfa
模版题
我用双向广搜没写出来…..

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#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(int x,int y,int z) {
        num=x;
        cost=y;
        step=z;
    }
} a[maxn];

int x[11];
int y[11];
int z[11];
int n,m;
int cost,step;

bool in(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*10+i;
            if(in(num)) {
                if(a[num].cost>(a[p.num].cost+x[i])) {
                    a[num].cost=a[p.num].cost+x[i];
                    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;
            if(in(num)){
                if(a[num].cost>(a[p.num].cost+y[i])) {
                    a[num].cost=a[p.num].cost+y[i];
                    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;
            if(in(num)){
                if(a[num].cost>(a[p.num].cost+z[i])) {
                    a[num].cost=a[p.num].cost+z[i];
                    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_JUDGE
//    freopen("IC.txt","r",stdin);
    freopen("i.in","r",stdin);
    freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
    while(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);
    }
    return 0;
}

/*


*/

Interesting Calculator 湖南第九届省赛

标签:

原文地址:http://blog.csdn.net/mymilkbottles/article/details/51345313

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!