#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int MaxLoading(int w[],int c,int n)
{
queue<int>Q;
int bestw=0;
int i=1;
int ew=0;
int r=0;
for(int j=2;j<=n;j++)
r=r+w[j];
Q.push(-1);
while(true)
{
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=r-w[i];
}
}
}
int main()
{
int w[100];
int n,c;
cout<<"输入物品的个数和船的载重量"<<endl;
cin>>n>>c;
cout<<"输入物品的质量"<<endl;
for(int i=1; i<=n; i++)
cin>>w[i];
cout<<"最大质量为 "<<MaxLoading(w,c,n)<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef struct QNode
{
QNode *parent;
int lchild;
int weight;
} QNode;
int n;
int c;
int bestw;
int w[100];
int bestx[100];
//QNode *&bestE 的原因是 首先bestE是个地址, 其次引用为了赋值使用, 后边for循环中用到
void EnQueue(queue<QNode *> &q, int wt, int i, QNode *E, QNode *&bestE, int ch)
{
if(i == n)
{
if(wt == bestw)
{
bestE = E;
bestx[n] = ch;
return;
}
}
QNode *b=new QNode;
b->weight = wt;
b->lchild = ch;
b->parent = E;
q.push(b);
}
void MaxLoading()
{
queue<QNode *>q;
q.push(0);
int i = 1;
int Ew = 0, r = 0;
bestw = 0;
for(int j = 2; j <= n; ++j)
r += w[j];
QNode *E, *bestE; //bestE的作用是:结束while循环后,bestE指向最优解的叶子节点,然后通过bestE->parent找到装入了哪些物品。
E = new QNode; //E这里作为一个中间量,连接parent和child
E = 0; //赋0是因为树的根的值是0,while刚开始的时候其代表root
while(true)
{
int wt = Ew + w[i];
if(wt <= c)
{
if(wt > bestw)
bestw = wt;
EnQueue(q, wt, i, E, bestE, 1);
}
if(Ew + r > bestw)
{
EnQueue(q, Ew, i, E, bestE, 0);
}
E = q.front();
q.pop();
if(E==0)
{
if(q.empty())
break;
q.push(0);
E = q.front();
q.pop();
i++;
r -= w[i];
}
Ew = E->weight; //不要忘记更新最新节点的值
}
for(int j = n - 1; j > 0; j--)
{
bestx[j] = bestE->lchild;
bestE = bestE->parent;
}
}
int main()
{
cout<<"输入物品的个数和船的载重量"<<endl;
cin>>n>>c;
cout<<"输入物品的质量"<<endl;
for(int i=1; i<=n; i++)
cin>>w[i];
MaxLoading();
cout<<"最大质量为 "<<bestw<<endl;
cout<<"装载的物品为 \n";
for(int i = 1; i <= n; ++i)
cout<<bestx[i]<<" ";
cout<<endl;
/*for(int i = 1; i <= n; ++i)
if(bestx[i] == 1)
printf("%d ", i);*/
return 0;
}
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f
/*bool operator < (const int &a,const int &b) const
{
return a<b;
}*/
queue<int>Q;
void EnQueue(int wt,int bestw,int i,int n)
{
if(i==n)
{
if(wt>bestw)
{
bestw=wt;
}
}
else
Q.push(wt);
}
int MaxLoading(int w[],int c,int n)
{
int i=1;
int ew=0;
int bestw=0;
Q.push(ew);
Q.push(-1);
while(true)
{
if(ew+w[i]<=c)
{
EnQueue(ew+w[i],bestw,i,n);
EnQueue(ew,bestw,i,n);
Q.pop();
ew=Q.front();
if(ew==-1)
{
Q.pop();
if(Q.empty())
return bestw;
Q.push(-1);
ew=Q.front();
i++;
}
}
}
}
int main()
{
int w[100];
int n,c;
cout<<"输入物品的个数和船的载重量"<<endl;
cin>>n>>c;
cout<<"输入物品的质量"<<endl;
for(int i=1;i<=n;i++)
cin>>w[i];
cout<<"最大质量为 "<<MaxLoading(w,c,n)<<endl;
return 0;
}
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f
/*bool operator < (const int &a,const int &b) const
{
return a<b;
}*/
void EnQueue(queue<int>&Q,int wt,int bestw,int i,int n)
{
if(i==n)
{
if(wt>bestw)
{
bestw=wt;
}
}
else
Q.push(wt);
}int bestw;
int MaxLoading(queue<int>&Q,int w[],int c,int n)
{
int i=1;
int ew=0;
int r=0;
for(int j=2;j<=n;j++)
r=r+w[j];
//Q.push(ew);
Q.push(-1);
while(true)
{
if(ew+w[i]<=c)
{
EnQueue(Q,ew+w[i],bestw,i,n);
}
EnQueue(Q,ew,bestw,i,n);
//Q.pop();
ew=Q.front();
Q.pop();
if(ew==-1)
{
//Q.pop();
if(Q.empty())
return bestw;
Q.push(-1);
ew=Q.front();
Q.pop();
i++;
}
}
}
int main()
{
bestw=0;
queue<int>Q;
int w[100];
int n,c;
cout<<"输入物品的个数和船的载重量"<<endl;
cin>>n>>c;
cout<<"输入物品的质量"<<endl;
for(int i=1; i<=n; i++)
cin>>w[i];
cout<<"最大质量为 "<<MaxLoading(Q,w,c,n)<<endl;
return 0;
}
来源:CSDN
作者:啦啦啦mmm
链接:https://blog.csdn.net/qq_45593796/article/details/103764655