#include<bits/stdc++.h>
using namespace std;
const int N=20;
int n;//物品总件数
int c;//船的最大装载量
int w[N];//记录每件物品重量
int bestw;//记录最佳重量
int r;//用于记录剩下的物品的总重量
int MaxLoading()
{
queue<int> q;//创建队列
q.push(-1);//首先将-1入队
int i = 0;
int Ew = 0;
while (1)
{
int wt = Ew+w[i];
if (wt<=c)
{
if (wt>bestw) bestw = wt;
if (i<n) q.push(wt);
}//左分支入队
if (Ew+r>bestw && i<n) q.push(Ew);//右分支入队
Ew = q.front();
q.pop();
if (Ew==-1)
{
if (q.empty()) return bestw;//如果此时全部物品都遍历过
q.push(-1);//设置换行条件
Ew = q.front();//取队头
q.pop();
i++;
r -= w[i];//更新剩余物品总重量
}//进入下一层
}
return bestw;
}
int main()
{
cin>>n>>c;//输入物品件数和船的最大装载量
for(int i=1;i<=n;i++){
cin>>w[i];
r+=w[i];
}
int ans=MaxLoading();
cout<<ans;
return 0;
}
05-19
5903
06-16
01-02
611
12-27
5529
08-06
3419
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交