首页 > 代码库 > BAPC2014 B&&HUNNU11582:Button Bashing(BFS)

BAPC2014 B&&HUNNU11582:Button Bashing(BFS)

题意:

给出n,m,代表微波炉有n个button,要求达到总时间为m

然后给出n个数。代表n个button能添加的时间,问最少几步,可以使得按出的总时间大于等于要求的时间,而且相差最小

输出最小的步数与相差的最小值

要求,当总时间小于0时。时间为0,大于3600时,时间为3600


思路:

直接暴力BFS。用VIS记录步数


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define ls 2*i
#define rs 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define ULL unsigned long long
#define N 100005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
const int mod = 1000000007;

int t,n,sum;
int a[20];
int vis[N];

int main()
{
    int i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&sum);
        for(i = 0; i<n; i++)
            scanf("%d",&a[i]);
        MEM(vis,INF);
        queue<int> Q;
        Q.push(0);
        vis[0] = 0;
        while(!Q.empty())
        {
            int x = Q.front();
            Q.pop();
            for(i = 0; i<n; i++)
            {
                int next = x+a[i];
                if(next<0) next = 0;
                if(next>3600) next = 3600;
                if(vis[next]<=vis[x]+1) continue;
                vis[next] = vis[x]+1;
                Q.push(next);
            }
        }
        for(i = sum; i<=3600; i++)
        {
            if(vis[i]!=INF)
            {
                break;
            }
        }
        printf("%d %d\n",vis[i],i-sum);
    }

    return 0;
}


BAPC2014 B&amp;&amp;HUNNU11582:Button Bashing(BFS)