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

CSU 1336: Interesting Calculator(BFS啊 湖南省第九届大学生计算机程序设计竞赛)

时间:2014-11-22 20:17:16      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:csu   bfs   

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336


1336: Interesting Calculator

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

HINT

Source


题意:

给定两个数字 x 和 y !

通过三种操作

1、直接在当前显示的数字后面添加一位数

2、在当前显示的数字的基础上增加

3、把当前显示的数字变为其n倍

求从x变为y所需要的最少的花费(每一步操作有相应的花费),如果花费相同,选取操作次数最少的!


PS:

此题直接BFS一遍能走到的所有点,更新为最小的花费!有点Floyd的思路参杂其中!

代码如下:

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 100017;
queue<int >Q;
int step;
int cont[maxn], dis[maxn], vis[maxn];
int v[7][maxn];

void cal(int s, int tt, int cost)
{
	if(dis[tt]==dis[s]+cost && cont[tt]>cont[s]+1)
	{
		cont[tt] = cont[s]+1;
		if(!vis[tt])
		{
			Q.push(tt);
			vis[tt] = 1;
		}
	}
	else if(dis[tt] > dis[s]+cost)
	{
		dis[tt] = dis[s]+cost;
		cont[tt] = cont[s]+1;
		if(!vis[tt])
		{
			Q.push(tt);
			vis[tt] = 1;
		}
	}
}

void BFS(int x, int y)
{
	int i, j;
	for(i = 0; i < maxn; i++)
	{
		vis[i]=0;
		dis[i]=INF;
		cont[i]=INF;
	}
	Q.push(x);
	vis[x] = 1;
	cont[x] = 0;
	dis[x] = 0;
	while(!Q.empty())
	{
		int t = Q.front();
		Q.pop();
		for(i = 0; i < 10; i++)
		{
			if(t*10+i < maxn)//直接在后面增加一位数
				cal(t,t*10+i,v[0][i]);
			if(t+i < maxn)//原值增加
				cal(t,t+i,v[1][i]);
			if(t*i < maxn)//倍数
				cal(t,t*i,v[2][i]);
		}
	}
}

int main()
{
	int cas = 0;
	int x, y;
	while(~scanf("%d%d",&x,&y))
	{
		while(!Q.empty())
		{
			Q.pop();
		}
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 10; j++)
			{
				scanf("%d",&v[i][j]);
			}
		}
		BFS(x,y);
		printf("Case %d: %d %d\n",++cas,dis[y],cont[y]);
	}
	return 0;
}

/*
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 
*/


CSU 1336: Interesting Calculator(BFS啊 湖南省第九届大学生计算机程序设计竞赛)

标签:csu   bfs   

原文地址:http://blog.csdn.net/u012860063/article/details/41384253

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