问题一解题思路
这里和上面博客计算的不太一样,贴出matlab代码
f=-[7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
7.592992;1.157625;5.71234;5.0673;2.40597;0.709632;0.214032;2.47;2.8704;1.5;
]';
a1=[7.592992 1.157625 5.71234 5.0673 2.40597 0.709632 0.214032 2.47 2.8704 1.5];
A=zeros(18,90);
for i=1:9
for k=1:10
A(i,(i-1)*10+k)=a1(k);
end
end
a2=[2.1 0.2 0.7 1.8 1.3 0.3 0.23 1.2 0.9 0.3];
for i=10:18
for k=1:10
A(i,(i-9-1)*10+k)=a2(k);
end
end
b=[2038.14;2501.2;1703.52;838.44;1321.776;691.028;117.3;140.76;105.57;
10;16;8;8;12;6;6;8;4];
a=[2.1 0.2 0.7 1.8 1.3 0.3 0.23 1.2 0.9 0.3];
Aeq=[
a/10 -a/16 zeros(1,70);
a/10 zeros(1,10) -a/8 zeros(1,60);
zeros(1,30) a/8 -a/12 zeros(1,40);
zeros(1,30) a/8 zeros(1,10) -a/6 zeros(1,30);
zeros(1,60) a/6 -a/8 zeros(1,10);
zeros(1,60) a/6 zeros(1,10) -a/4];
beq=zeros(6,1);
lb=zeros(9,10);
intcon=1:1:90;
[x,fval,exitflag]=intlinprog(f,intcon,A,b,Aeq,beq,lb,[]);
x0(:,1)=x(1:10,1);
for i=2:9
x0(:,i)=x((i-1)*10+1:i*10,1);
end
for i=1:10
x1(i,:)=x0(i,:)*a(i);
x3(i,:)=x0(i,:)*a1(i);
end
x2=sum(x1);%重量和
x4=sum(x3);%体积和
使用matlab计算出将小于2立方米的货物装进集装箱的情况
f=-[1.157625;0.709632;0.214032;1.5];
f=[f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;
f;f;f;f;f;f;f;f;f;f;f;f;];
a=[1.157625 0.709632 0.214032 1.5];
A=zeros(120,480);
for i=1:120
for j=1:4
A(i,4*(i-1)+j)=a(j);
end
end
b1=15.8029114*ones(40,1);
b2=5.7065904*ones(30,1);
b3=15.8029114*ones(10,1);
b4=16.4788272*ones(20,1);
b5=6.041392*ones(10,1);
b6=11.28561*ones(10,1);
b=[b1;b2;b3;b4;b5;b6];
Aeq=zeros(4,480);
for i=1:120
for k=1:4
Aeq(k,4*(i-1)+k)=1;
end
end
A=[A;Aeq];
beq=[368;307;611;1225];
b=[b;beq];
intcon=1:1:480;
lb=zeros(480,1);
[x,fval,exitflag]=linprog(f,A,b,[],[],lb,[]);
x=round(x);
x0(:,1)=x(1:4,1);
for i=2:120
x0(:,i)=x((i-1)*4+1:i*4,1);%对应的货物数量
end
sum(x0,2);
忽略所有没装进集装箱的2立方米以下的货物,得到剩余6种货物和8种集装箱共14种货物装飞机。
使用C++代码计算出大中小飞机将所有货物运走需要多少架,计算得出大的190,中的249,小的由于有一个货物大于10t所以装不了
#include
#include
using namespace std;
const int N = 1e5 + 10;
int flagv[N], flagw[N];
int res;
struct goods {
double v = 0;
double w = 0;
int nums = 0;
goods() {}
goods(const double x, const double y, const int z)
{
v = x;
w = y;
nums = z;
}
}g[14];
struct plane {
double wa;
double wm;
double wb;
double va;
double vm;
double vb;
plane(double x, double y, double z, double m, double n, double k) :wa(x), wm(y), wb(z), va(m), vm(n), vb(k) {}
};
int main()
{
//ofstream outfile("D:\\桌面文件\\C语言\\OJ\\1.txt");
//plane pbig = plane(10, 16, 8, 2038.14, 2501.2, 1703.52);
//plane pmid = plane(8, 12, 6, 838.44, 1321.776, 691.028);
//plane psma = plane(6, 8, 4, 117.3, 140.76, 105.57);
g[0] = goods(7.59, 2.1, 119);
g[1] = goods(5.71, 0.7, 361);
g[2] = goods(5.06, 1.8, 364);
g[3] = goods(2.41, 1.3, 247);
g[4] = goods(2.47, 1.2, 2993);
g[5] = goods(2.87, 0.9, 617);
g[6] = goods(17.77, 3.07, 30);//货箱
g[7] = goods(17.77, 3.3, 10);
g[8] = goods(6.74, 1.05, 30);
g[9] = goods(17.77, 3.15, 10);
g[10] = goods(18.43, 3.28, 10);
g[11] = goods(18.43, 11.7, 10);
g[12] = goods(7.12, 3.97, 10);
g[13] = goods(12.88, 6.9, 10);
while (g[0].nums != 0 || g[1].nums != 0 || g[2].nums != 0 || g[3].nums != 0 || g[4].nums != 0 || g[5].nums != 0 || g[6].nums != 0 || g[7].nums != 0 || g[8].nums != 0 || g[9].nums != 0 || g[10].nums != 0 || g[11].nums != 0 || g[12].nums != 0 || g[13].nums != 0) {
plane pbig = plane(6, 8, 4, 117.3, 140.76, 105.57);//这里要换不同的飞机
for (int i = 0; i < 14; ++i)
{
if (g[i].nums <= 0) continue;
while(pbig.wa - g[i].w >= 0 && pbig.va - g[i].v >= 0)
{
if (g[i].nums != 0)
{
pbig.wa = pbig.wa - g[i].w;
pbig.va = pbig.va - g[i].v;
g[i].nums--;
}
else break;
}
while(pbig.wm - g[i].w >= 0 && pbig.vm - g[i].v >= 0)
{
if (g[i].nums != 0)
{
pbig.wm = pbig.wm - g[i].w;
pbig.vm = pbig.vm - g[i].v;
g[i].nums--;
}
else break;
}
while(pbig.wb - g[i].w >= 0 && pbig.vb - g[i].v >= 0)
{
if (g[i].nums != 0)
{
pbig.wb = pbig.wb - g[i].w;
pbig.vb = pbig.vb - g[i].v;
g[i].nums--;
}
else break;
}
}
res++;
cout << g[0].nums << " " << g[1].nums << " " << g[2].nums << " " << g[3].nums << " " << g[4].nums << " " << g[5].nums << " " << g[6].nums << " " << g[7].nums << " " << g[8].nums << " " << g[9].nums << " " << g[10].nums << " " << g[11].nums << " " << g[12].nums << " " << g[13].nums << " ";
cout << "结果是" << res << endl;
}
cout <<"结果是"<< res;
}