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

BFS CSU

时间:2014-11-24 22:41:25      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:bfs

因为要花费最少,如果花费最少的有多个还要使得步数最少

所以在判断一个数字要不要入队列的时候只要判断这个就可以了

I - Interesting Calculator
Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

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 contains10 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 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 

Sample Output

Case 1: 2 2Case 2: 12 3 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <map>
using namespace std;
#define MAXN 100000 + 100
#define INF 999999999

int cost[MAXN];
int con[MAXN];
int num[15][15];
int x,y;
int cas=1;

void BFS(){
    int i,j;
    queue<int>q;
    //vis[x] = true;
    cost[x] = 0;
    q.push(x);
    while(!q.empty()){
        int tt = q.front();
        //vis[tt] = false;
        q.pop();
        int dx;
        for(i=0;i<3;i++){
            for(j=0;j<10;j++){
                if(i == 0){
                    dx = tt*10+j;
                }
                if(i == 1){
                    dx = tt + j;
                }
                if(i == 2){
                    dx = tt*j;
                }
                if(dx > y){
                    continue;
                }
                if((cost[dx]>cost[tt]+num[i][j]) || (cost[dx]==cost[tt]+num[i][j]&&con[dx]>con[tt]+1)){
                    cost[dx] = cost[tt] + num[i][j];
                    con[dx] = con[tt] + 1;
                    /*if(vis[dx] == false){
                        q.push(dx);
                        vis[dx] = true;
                    }*/
                    q.push(dx);
                }
            }
        }
    }
}

int main(){
    int i,j;

    while(~scanf("%d%d",&x,&y)){
        //memset(vis,false,sizeof(false));
        memset(con,0,sizeof(con));
        for(i=0;i<MAXN;i++){
            cost[i] = INF;
        }
        for(i=0;i<3;i++){
            for(j=0;j<10;j++){
                cin>>num[i][j];
            }
        }
        BFS();
        printf("Case %d: %d %d\n",cas++,cost[y],con[y]);
    }

    return 0;
}


BFS CSU

标签:bfs

原文地址:http://blog.csdn.net/zcr_7/article/details/41451429

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