minZ=x1+x2+x3
s.t3x1+x2+x3>=1
-x1+4x2+x3>=2
x1,x2,x3>=0
先将其化为标准型
minZ=x1+x2+x3
s.t3x1+x2+x3-x4=1
-x1+4x2+x3-x5=2
x1,x2,x3>=0
请输入方程组的系数矩阵A(2行5列):
3 1 1 -1 0
-1 4 1 0 -1
请输入初始基变量的数字代码num矩阵:
4 5
请输入方程组右边的值矩阵b:
1 2
请输入目标函数各个变量的系数所构成的系数阵C:
1 1 1 0 0
--------------------------------------------------------------------------
X(1)X(2)X(3)X(4)X(5)RHS
--------------------------------------------------------------------------
-1.000 -1.000-1.000 0.0000.000 0.000
--------------------------------------------------------------------------
x(4)-3.000 -1.000-1.000 1.0000.000 -1.000
x(5)1.000 -4.000-1.000 0.0001.000 -2.000
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-1.250 0.000-0.750 0.000-0.250 0.500
--------------------------------------------------------------------------
x(4)-3.250 0.000-0.750 1.000-0.250 -0.500
x(2)-0.250 1.0000.2500.000 -0.2500.500
--------------------------------------------------------------------------
所得解已经是最优解!
--------------------------------------------------------------------------
0.0000.000 -0.462-0.385 -0.154 0.692
--------------------------------------------------------------------------
x(1)1.0000.0000.231 -0.3080.077 0.154
x(2)0.0001.0000.308 -0.077-0.231 0.538
--------------------------------------------------------------------------
x(1)=0.154x(2)=0.538z=0.692Press any key to continue
#include<stdio.h>
#include<math.h>
#define m 2
#define n 5
float M=1000000.0;
float A[m][n];
floatC[n];
floatb[m];
float seta[n];
intnum[n];
float z=0;
void input();
void print();
int duioudanchunxing1();
int duioudanchunxing2(int a);
void duioudanchunxing3(int a,int b);
void input()
{
int i,j;
printf("请输入方程组的系数矩阵A(%d行%d列):n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",&A[i][j]);
printf("n请输入初始基变量的数字代码num矩阵:n");
for(i=0;i<m;i++)
scanf("%d",&num[i]);
printf("n请输入方程组右边的值矩阵b:n");
for(i=0;i<m;i++)
scanf("%f",&b[i]);
printf("n请输入目标函数各个变量的系数所构成的系数阵C:n");
for(i=0;i<n;i++)
scanf("%f",&C[i]);
}
intduioudanchunxing1()
{
int i,k;
int flag;
float min=0;
for(i=0;i<m;i++)
if(b[i]>=0)
flag=1;
else {flag=0;break;}
if(flag==1)
return -1;
for(i=0;i<m;i++)
{
if(min>b[i])
{min=b[i];k=i;}
}
return k;
}
intduioudanchunxing2(int a)
{
int i,j;
int flag=0;
float min;
for(j=0;j<n;j++)
if(A[a][j]>=0)
flag=1;
else {flag=0;break;}
if(flag==1)
{printf("n该线性规划无最优解!n"); return -1;}
for(j=0;j<n;j++)
{
if(A[a][j]<0)
seta[j]=-C[j]/A[a][j];
else seta[j]=M;
}
min=M;
for(j=0;j<n;j++)
{
if(min>=seta[j])
{min=seta[j];i=j;}
}
num[a]=i+1;
return i;
}
voidduioudanchunxing3(int p,int q)
{
int i,j,c,l;
float temp1,temp2,temp3;
c=q;
l=p;
temp1=A[c][l];
b[c]=b[c]/temp1;
for(j=0;j<n;j++)
A[c][j]=A[c][j]/temp1;
for(i=0;i<m;i++)
{
if(i!=c)
if(A[c][l]!=0)
{
temp2=A[i][l];
b[i]=b[i]-b[c]*temp2;
for(j=0;j<n;j++)
A[i][j]=A[i][j]-A[c][j]*temp2;
}
}
temp3=C[l];
for(i=0;i<n;i++)
C[i]=C[i]-A[l][i]*temp3;
z=z+b[c]*temp3;
}
void print()
{
int i,j;
printf("n--------------------------------------------------------------------------n");
printf("t");
for(i=0;i<n;i++)
{
printf("%.3ft",-C[i]);
}
printf("%.3f",z);
printf("n--------------------------------------------------------------------------n");
for(i=0;i<m;i++)
{
printf("x(%d)t",num[i]);
for(j=0;j<n;j++)
printf("%.3ft",A[i][j]);
printf("%.3fn",b[i]);
}
printf("n--------------------------------------------------------------------------n");
}
main()
{
int i,j=0;
int p,q;
input();
for(i=0;i<m;i++)
{
if(A[i][num[i]-1]<=0)
{
b[i]=-b[i];
for(j=0;j<n;j++)
A[i][j]=-A[i][j];
}
}
printf("n--------------------------------------------------------------------------n");
printf("t");
for(i=0;i<n;i++)
printf("X(%d)t",i+1);printf("RHSn");
while(1)
{
q=duioudanchunxing1();
if(q==-1)
{
printf("n所得解已经是最优解!n");
print();
printf("x(%d)=%.3ft",num[i],b[i]);
printf("z=%.3f",z);
break;
}
print();
p=duioudanchunxing2(q);
if(q==-1) break;
duioudanchunxing3(p,q);
}
}