POJ Bookshelf 2(DFS)

Bookshelf 2
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5109 Accepted: 2350

Description

Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

FJ has N cows (1 ≤ N ≤ 20) each with some height of Hi (1 ≤ Hi ≤ 1,000,000 - these are very tall cows). The bookshelf has a height of B (1 ≤ BS, where S is the sum of the heights of all cows).

To reach the top of the bookshelf, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height of the bookshelf in order for the cows to reach the top.

Since a taller stack of cows than necessary can be dangerous, your job is to find the set of cows that produces a stack of the smallest height possible such that the stack can reach the bookshelf. Your program should print the minimal 'excess' height between the optimal stack of cows and the bookshelf.

Input

* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer: Hi

Output

* Line 1: A single integer representing the (non-negative) difference between the total height of the optimal set of cows and the height of the shelf.

Sample Input

5 16
3
1
3
5
6

Sample Output

1

Source

//这题和上次ZOJ月赛的一题差不多,我都是用收索过的
//因为N比较小
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> using namespace std; int a[22]; int N,B,Min; bool cmp(const int&a,const int &b) { return a>b; } void dfs(int k,int sum) {
if(Min==0) return;//加了这句就0Ms,不然就16Ms
if(sum>=B) { Min=min(Min,sum-B); return ; } int i,s=0; for(i=k;i<N;i++) s+=a[i]; if(sum+s<B) return; dfs(k+1,sum+a[k]); dfs(k+1,sum); } int main() { int i; while(scanf("%d%d",&N,&B)!=EOF) { Min=B; for(i=0;i<N;i++) scanf("%d",&a[i]); sort(a,a+N,cmp); dfs(0,0); printf("%d\n",Min); } return 0; }

转载于:https://www.cnblogs.com/372465774y/archive/2012/08/06/2624536.html

内容概要:本文详细介绍了FLAC3D6.0软件中关于巷道开挖、支护与充填开采的三组源代码及其应用场景。首先,针对巷道开挖部分,通过设定合理的边界条件和采用适当的命令如zone relax excavate来模拟开挖后地压对巷道稳定性的动态影响。其次,在巷道锚杆支护方面,强调了锚杆与围岩之间的耦合关系以及相关参数的精确设置,确保支护的有效性和可靠性。最后,对于工作面充填开采,则重点探讨了充填体的存在与否对上覆岩层应力场、位移场和破坏场的具体影响,并提供了具体的代码实现方法。此外,还分享了一些实用的小技巧,比如利用save [filename]和restore命令进行参数敏感性分析,提高了计算效率。 适合人群:从事岩土工程领域的研究人员和技术人员,尤其是那些希望深入了解FLAC3D6.0软件在巷道开挖、支护与充填开采方面的应用的人士。 使用场景及目标:适用于需要进行巷道开挖、支护与充填开采数值模拟的研究项目。主要目的是帮助用户掌握FLAC3D6.0的相关命令和参数配置,以便更好地理解和预测实际工程中的地质力学行为。 其他说明:文中提供的代码均带有详细的汉语注释,便于初学者学习和理解。同时,作者提醒使用者要注意一些常见的陷阱,如边界条件的选择、锚杆耦合等问题,以避免计算过程中出现不必要的错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值