admin 管理员组

文章数量: 887032

选址

#include<iostream>  
#include<cmath>  
using namespace std;int n;             //楼的栋数
double maxx,maxy,minx,miny;            //楼坐标的最大和最小横纵坐标 
double midx1,midx2,midy1,midy2;
double ox,oy;             //便利店的横纵坐标
double sum;
struct node
{double x;double y;int quan;
};
node lou[100];double value(double x,double y)
{double s=0;for(int i=0;i<n;i++)s+=sqrt((x-lou[i].x)*(x-lou[i].x)+(y-lou[i].y)*(y-lou[i].y))*lou[i].quan;return s;
}double sanfen_x(double y)
{double up_x,down_x;up_x=maxx;down_x=minx;midx1=(up_x+down_x)/2;midx2=(up_x+midx1)/2;while((up_x-down_x)>0.01){if(value(midx1,y)>value(midx2,y))down_x=midx1;elseup_x=midx2;midx1=(up_x+down_x)/2;midx2=(up_x+midx1)/2;};return value(midx1,y);
}void sanfen_y()
{midy1=(maxy+miny)/2;midy2=(midy1+maxy)/2;while((maxy-miny)>0.01){if(sanfen_x(midy1)>sanfen_x(midy2))miny=midy1;elsemaxy=midy2;midy1=(maxy+miny)/2;midy2=(midy1+maxy)/2;};ox=midx1;oy=midy1;sum=value(ox,oy);
}void main()
{cout<<"请输入楼的栋数:";cin>>n;cout<<"请输入各个楼房的横纵坐标和住户数:"<<endl;minx=miny=maxx=maxy=0;for(int i=0;i<n;i++){cin>>lou[i].x>>lou[i].y>>lou[i].quan;if(lou[i].x>maxx)maxx=lou[i].x;if(lou[i].x<minx)minx=lou[i].x;if(lou[i].y>maxy)maxy=lou[i].y;if(lou[i].y<miny)miny=lou[i].y;}sanfen_y();cout<<"便利店的最佳横纵坐标为:"<<endl;cout<<"("<<ox<<","<<oy<<")"<<endl;cout<<"便利店的最优解为:"<<endl;cout<<sum<<endl;
}/*
8  
10 20 30  
30 34 45  
19 25 28  
38 49.1 8  
9 38.1 36  
2 34 16  
5 8 78  
29 48 56 */ 

便利店选址:

某小区决定在小区内部建一家便利店,现小区内部共有八栋楼,它们的地理坐标分别为:(10,20) (30,34) (19,25) (38,49.1) (9,38.1) (2,34) (5,8) (29,48)。同时,其中的住户人数分别为:30, 45, 28, 8, 36, 16, 78, 56。为了方便更多的住户购物,要求实现总体最优,请问便利店应该建立在哪里?


本文标签: 选址