BAPC2014 B&&HUNNU11582:Button Bashing(BFS)

题意:

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

然后给出n个数,代表n个按钮能增加的时间,问最少几步,能够使得按出的总时间大于等于要求的时间,并且相差最小

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

要求,当总时间小于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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 15:55:22

BAPC2014 B&&HUNNU11582:Button Bashing(BFS)的相关文章

BAPC2014 B&amp;amp;&amp;amp;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 <sta

Left Mouse Button (bfs)

Mine sweeper is a very popular small game in Windows operating system. The object of the game is to find mines, and mark them out. You mark them by clicking your right mouse button. Then you will place a little flag where you think the mine is. You c

BFS CSU

因为要花费最少,如果花费最少的有多个还要使得步数最少 所以在判断一个数字要不要入队列的时候只要判断这个就可以了 I - Interesting Calculator Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice CSU 1336 Description There is an interesting calculator. It has 3 ro

E - A strange lift 【BFS】

There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up K

D - Interesting Calculator 【BFS+优先队列】

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 disp

ZOJ 3865 Superbot BFS

地图很小,根据题意BFS Superbot Time Limit: 2 Seconds      Memory Limit: 65536 KB Superbot is an interesting game which you need to control the robot on an N*M grid map. As you see, it's just a simple game: there is a control panel with four direction left (1s

CSU 1336: Interesting Calculator(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

hdu1548 A strange lift (简单bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15974    Accepted Submission(s): 5973 Problem Description There is a strange l

HDU 1548 A strange lift(最短路&amp;&amp;bfs)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26943    Accepted Submission(s): 9699 Problem Description There is a strange lift.The lift can stop can at every floor as you want,