Benelux Algorithm Programming Contest 2014 Final B:Button Bashing

You recently acquired a new microwave, and noticed that it provides a large number of buttons to be able to quickly specify the time that the microwave should be running for. There are buttons both for adding time, and for subtracting time. You wonder how efficient you can be when entering cooking times: you want to minimize the number of required button presses.

The microwave can be running for at least 0 seconds, and at most 1 hour. If a button press would result in a cooking time of less than 0 seconds, the microwave will set the cooking time to 0 seconds. If a button press would result in a cooking time of more than 1 hour, the microwave will set the cooking time to 1 hour. Initially, the microwave will run for 0 seconds. There will always be a button adding at least 1 second to the cooking time.

Given the buttons that the microwave provides for entering cooking times, determine the least amount of button presses required to let the microwave run for a certain amount of time. If it is not possible to enter the desired cooking time precisely, determine the smallest achievable cooking time above the target, and the minimum number of button presses required for that cooking time, instead. The microwave does not allow to adjust the cooking time once it has started cooking.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with two space-separated integers n and t (1 ≤ n ≤ 16 and 0 ≤ t ≤ 3600): the number of buttons available to change the cooking time, and the desired cooking time in seconds, respectively.

  • one line with n space-separated integers bi (-3600 ≤ bi ≤ 3600): the number of seconds added to the cooking time when button i is pressed.

Output

Per test case:

one line with two space-separated integers: the minimum number of button presses required to reach the required cooking time, and the minimum number of extra seconds that the microwave must be running for, respectively.

Sample Input

2
3 50
-10 10 60
1 50
20

Sample Output

2 0
3 10



此题为bfs,首先先找出按一次能得到的值并记录按的按钮数,紧接着按第二次能得到的值,

并判断此时的值在之前有没有出现过,若有,不入队,因为之前出现的肯定按钮数更少

入队更新了。若之前没有出现过,那就入队,并在之前到达当前值item的步数加1。

代码:

#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
    int rode[4000];
 
    int n,m,num[20],Case;
    scanf("%d",&Case);
    while(Case--)
    {
         queue<int> que;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        memset(rode,INF,sizeof(rode));
        que.push(0);
        rode[0]=0;
        while(!que.empty())
        {
            int item=que.front();
            que.pop();
            for(int i=1;i<=n;i++)///此bfs处,
            {
                int result=item+num[i];
                if(result<0) result=0;
                else if(result>3600) result=3600;
                if(rode[result]<=rode[item]+1)
                    continue;///说明值result在更早的时候已经得到了,所以不需入队,不需更新步数
                que.push(result);
                rode[result]=rode[item]+1;///说明值result在此之前还没有得到过,此事值result的步数就为当前到达值item
                                        ///的步数加1
            }
        }
        int t;
        for( t=m;t<=3600;t++)
            if(rode[t]!=INF){
        break;}
        printf("%d %d\n",rode[t],t-m);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36561697/article/details/81023064