# ZOJ 3631-Watashi's BG（bfs+循环队列）

111人阅读 评论(0)
A - Watashi's BG
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Appoint description:

Description

Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer training camp, students are divided into several groups and each day one of the groups will design some problems to hold a contest. Today students of Group C are required to design the problems, and they spent the whole night to check to test data which made them very tired. Watashi decides to give some money as a reward to group C so that they can buy the lunch for free.

There are N days in the training schedule, and all students have booked their lunch for N days so we know how much money they will spend in each day. Now the leader of group C needs to decide how to use Watashi's money. Since the money is limited, it may not be possible that they can have free lunch every day. So each day the leader can choose to pay for the whole group's lunch by themselves or use Watashi's money. Of course, the leader wants to spend Watashi's money as much as possible, but he is too busy to write a program to calculate the maximum money he can spend from Watashi's reward. Can you help him?

Input

The input contains multiple test cases ( no more than 50 test cases ).
In each test case, first there are two integer, N ( 1 <= N <=30 ) , which is the number of training days, M ( 0 <= M <=10000000 ) , which is the reward money from Watashi.
Then there is a line containing N positive integers with the ith integer indicating the money group C need to pay for the lunch of the ithday. All these integers are no more than 10000000 and integers are seperated by a space.

Output

For each test case, output one line with an integer which is the maximum money group C can spend from Watashi's reward

Sample Input

3 10
8 4 5


Sample Output

9

思路：
   这题的时间是不能用dfs的会卡时间，所以就想到了要用bfs来做，因为这题只有30位，最后写好了超时了，原来没排序，排了各种wa，但是思路没错啊，不可能会Wa的，所以将数组开大，一开始500万，还是wa，之后1000万，MEL了，之后800万显示越界了，就在这时想到了循环队列，没想到AC了。2620ms。。。。。

AC代码：

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
typedef long long ll;
using namespace std;
#define T 8000000
#define inf 0x3f3f3f3f
int n,m;
int v[35];
struct node
{
int i;
int v;
node(){}
node(int _i,int _v):i(_i),v(_v){}
}p;
node q[T];
int bfs()
{
int ma =0;
{
if(p.v==m)return m;
if(p.v<m)ma=max(p.v,ma);
for(int i=p.i+1;i<n;++i){
if(p.v+v[i]==m)return m;
if(p.v+v[i]>m)break;
q[(tail++)%T] = node(i,p.v+v[i]);
}
}
return ma;
}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;++i){
scanf("%d",&v[i]);
}
sort(v,v+n);
for(int i=0;i<n;++i){
p.v = v[i];
p.i = i;
q[tail++] = p;
}
printf("%d\n",bfs());
}
return 0;
}
1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：235396次
• 积分：10259
• 等级：
• 排名：第1642名
• 原创：777篇
• 转载：186篇
• 译文：1篇
• 评论：37条
友情链接
博客专栏
 爬虫之旅 文章：5篇 阅读：3074
 php框架开发 文章：1篇 阅读：203