1. 程式人生 > >BAPC2014 B&&HUNNU11582:Button Bashing(BFS)

BAPC2014 B&&HUNNU11582:Button Bashing(BFS)

div pop track sin 大於 clu class data for

題意:

給出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)