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。

循环队列 + 插入 + 删除 + 遍历等操作

#include using namespace std ; #define MAXSIZE 100 typedef struct Queue { char *base ; int ...
  • wenlovingliu
  • wenlovingliu
  • 2013年05月25日 19:24
  • 2557

循环队列及C语言实现<一>

循环队列是为了充分利用内存,进行数据操作的一种基本算法。具体实现方式可划分为:链式队列和静态队列,这里所谓的静态是指在一片连续的内存区域进行数据操作。本文只讲述静态队列,也是最简单的实现方式,静态队列...
  • JAZZSOLDIER
  • JAZZSOLDIER
  • 2016年09月21日 22:34
  • 3388

数据结构:循环队列(C语言实现)

生活中有很多队列的影子,比如打饭排队,买火车票排队问题等,可以说与时间相关的问题,一般都会涉及到队列问题;从生活中,可以抽象出队列的概念,队列就是一个能够实现“先进先出”的存储结构。队列分为链式队列和...
  • lpp0900320123
  • lpp0900320123
  • 2014年03月07日 19:15
  • 85980

zoj 3631 Watashi's BG

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4777   题目大意: 有n(1~30)天,第i天花费vi(1~...
  • gotoac
  • gotoac
  • 2013年04月17日 12:20
  • 489

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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3631 题目描述:
  • u013303743
  • u013303743
  • 2014年06月30日 09:06
  • 515

ZOJ 3631 Watashi's BG

Description Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer...
  • ultimater
  • ultimater
  • 2012年07月31日 07:16
  • 1355

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

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4777 题意:有n天,告诉你每天的花费,别人给你一笔资金m,你自己也有一...
  • zzzz40
  • zzzz40
  • 2014年08月29日 01:11
  • 861

ZJU 3631 - Watashi's BG

题目地址:  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3631   将所有元素分成两堆,分别枚举。   ...
  • diannaok
  • diannaok
  • 2012年07月29日 21:47
  • 674

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

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

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

ZOJ-2849-Attack of Panda Virus
  • loy_184548
  • loy_184548
  • 2016年01月30日 14:44
  • 557
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ZOJ 3631-Watashi's BG(bfs+循环队列)
举报原因:
原因补充:

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