#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#define LEN sizeof(struct inf)
struct inf
{int shop;
int goods;
int store;
int factory;
int price;
struct inf *next;
};
struct choose
{int shop;
int goods;
int store;
int factory;
int price;
};
void main()
{struct inf *p1,*p2,*p3;
struct choose zu[80];
struct inf *creat(int i,int k[8][10][3][5]);//创建链表
struct inf *delect(struct inf *head,int n);//删除链表
FILE *fp;
int bc[5][8]={3,2,3,6,3,1,4,5,0,3,3,0,5,2,5,3,5,0,2,5,0,5,0,4,4,1,4,4,2,0,2,0,2,2,5,3,5,2,5,2},//仓库到分店的运输费数组
ab[3][5]={0,10,14,17,13,10,8,0,9,16,15,16,9,15,0},//工厂到仓库的运输费数组
am[][10]={100,0,210,250,290,300,0,430,450,500,90,180,0,240,0,305,380,435,0,490,0,170,210,245,0,285,400,0,450,480};// 工厂生产十种商品的价格
int xu[8][10]={60,300,800,100,200,600,400,80,150,600,//各分店对各种产品的需求量
90,800,500,1200,500,400,200,100,800,500,
150,500,400,800,600,0,800,800,400,0,
300,400,200,400,150,800,500,150,1500,400,
400,0,150,100,200,300,0,400,90,800,
500,200,1000,0,400,150,1000,1000,200,400,
800,1200,90,150,90,1000,90,500,100,1000,
1500,200,500,500,1000,90,150,200,500,200};
float v[10]={1.5,1,1.5,2,1.5,0.5,1.5,2,1,0.5};//每件产品的体积
float b[5]={0,0,0,0,0},b1[5]={800,600,1000,700,800};//每个仓库的容量
int a1[3][10]={2000,0,3000,1000,3200,1000,0,2000,1500,1500,2000,1300,0,1000,
0,1500,2000,1500,0,1200,0,2500,800,1500,0,1000,1400,0,2500,1500};//每个工厂对每种商品的产量
//k[8][10][3][5]用来储存分店中产品的来源信息
int k[8][10][3][5];
int i,j,n,p,h,a[3]={0,0,0},l=0,c,min;
if((fp=fopen("num","w"))==NULL)
{printf("cannot open file\n");
}
//k[n][p][i][j]表示n分店中p种产品如果来自i工厂和j仓库所需的总花费
for(n=0;n<8;n++)
for(p=0;p<10;p++)
for(i=0;i<3;i++)
{ if(am[i][p]==0)
for(j=0;j<10;j++)
k[n][p][i][j]=0;
else for(j=0;j<10;j++)
k[n][p][i][j]=am[i][p]+ab[i][j]+bc[j][n];
}
for(i=0;i<10;i++)//每循化一次得到一种商品的进货和销售信息
{p1=creat(i,k);
p3=p1;
for(c=0;c<3;c++)
a[c]=0; //对工厂c生产的i种产品初始值为零
printf("hello\n");
for(h=0;h<8;h++)
{ p2=p1;min=600;
while(p2!=NULL)
{ if(p2->price!=0&&p2->price<min)//条件1:花费最少
if((xu[p2->shop-1][i]*v[i]/15+b[p2->store-1])<b1[p2->store-1]/15&&(xu[p2->shop-1][i]/15+a[p2->factory-1])<a1[p2->factory-1][i]/15)//条件2:工厂能满足供给且仓库可以存放下
{min=p2->price;n=p2->shop;
j=p2->factory;p=p2->store;}//满足以上两条件则按该方案进行采购和分配
p2=p2->next;
}
zu[l].shop=n;
zu[l].store=p;
zu[l].factory=j;
zu[l].goods=i;
zu[l].price=min;
b[n-1]=b[n-1]+xu[n-1][i]*v[i]/15;
a[j-1]=xu[n-1][i]/15+a[j-1];
printf("%d\n",n);
p1=delect(p1,n);// 删除多余的十四种方案
l++;
printf("%d %d %d|n",zu[l].price,zu[l].store=p,zu[l].factory=j);
}
}
//将最终信息显示在十进制文件上
fprintf(fp,"factory goods price shop store\n");
for(i=0;i<80;i++)
fprintf(fp,"%d %d %d %d %d\n",zu[i].factory,zu[i].goods+1m,zu[i].price,zu[i].shop,zu[i].store);
fclose(fp);
}
struct inf *creat(int i,int k[8][10][3][5])
{struct inf *head;
struct inf *p1,*p2;
int n,j,p,g=0;
p1=p2=(struct inf*)malloc(LEN);
head=NULL;
for(n=0;n<8;n++)
for(j=0;j<3;j++)
for(p=0;p<5;p++)
{g=g+1;
if(g==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct inf *)malloc(LEN);
p1->shop=n+1;p1->factory=j+1;p1->store=p+1;p1->price=k[n][i][j][p];p1->goods=i+1;
}
p2->next=NULL;
head=head->next;
printf("hello\n");
return(head);
}
struct inf *delect(struct inf *head,int n)
{struct inf *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");
}
p1=head;
while(n!=p1->shop&&p1->next!=NULL)
{ p2=p1;p1=p1->next;}
if(n==p1->shop)
while(n==p2->shop)
{if(p1==head) head=p1->next;
else p2->next=p1->next;}
else printf("n not been found\n");
return(head);
}