ZOJ 3631-Watashi's BG(bfs+循环队列)

原创 2015年11月20日 13:18:24
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 head,tail;
int bfs()
{	
	int ma =0;
	while(head!=tail)
	{
		p = q[(head++)%T];
		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))
 {
	 head =tail = 0;
	 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;
}


版权声明:本文为博主原创文章,转载必须声明出处,thank。

相关文章推荐

zoj 3631 Watashi's BG

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4777   题目大意: 有n(1~30)天,第i天花费vi(1~...

【DFS+剪枝】-ZOJ-3631-Watashi's BG

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3631 题目描述:

ZOJ 3631 Watashi's BG

Description Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer...

ZOJ--3631--Watashi's BG【枚举】

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4777 题意:有n天,告诉你每天的花费,别人给你一笔资金m,你自己也有一...

ZJU 3631 - Watashi's BG

题目地址:  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3631   将所有元素分成两堆,分别枚举。   ...

zoj 1091 Knight Moves (BFS)(情况用循环控制,值得学习啊)

刚开始看不懂题,后来查了查才明白这颗棋子是走“日”字的。。。。 接着,那就会出现8种情况。。。无语,不过,有了2165的模板,我居然慢悠悠的把这八种情况罗列出来并且AC了。。         上一...

zoj 3103 Cliff Climbing 优先队列+BFS

题目链接: 3103 题意: 一块N X M 的墙壁,求从S点出发 到T点的最短时间 每次只能爬一步,且只能左右脚交替爬行,墙上每个方块中的数字标明方块的"...

ZOJ-2849-Attack of Panda Virus【bfs】【优先级队列】【4th浙江省赛】【好题】

ZOJ-2849-Attack of Panda Virus

ZOJ 1649 && HDU 1242 Rescue (BFS + 优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M...

Hdu 1242 & Zoj 1649 Rescue (优先队列+BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:朋友(r)杀敌人(x)救天使(a),时间最短型走迷宫,走格子耗时1s,杀敌人额外耗时1s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)