数学建模快递问题c++
时间: 2023-10-18 10:02:45 浏览: 43
数学建模快递问题主要是要求根据已知数据进行模型构建和求解,其中需要用到一些算法和数据结构。以下是一个基于C++语言的模型构建和求解代码:
```c++
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1005;
const double INF = 1e9;
struct point{
double x,y;
}p[MAXN],st[MAXN],ans[MAXN];
int n,top;
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cross(point a,point b,point c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
bool cmp(point a,point b)
{
if(a.y!=b.y) return a.y<b.y;
return a.x<b.x;
}
bool comp(point a,point b)
{
return dis(ans[1],a)<dis(ans[1],b);
}
void graham()
{
sort(p+1,p+n+1,cmp);
st[++top]=p[1];
for(int i=2;i<=n;i++)
{
while(top>1&&cross(st[top-1],st[top],p[i])<=0) top--;
st[++top]=p[i];
}
int tmp=top;
for(int i=n-1;i>=1;i--)
{
while(top>tmp&&cross(st[top-1],st[top],p[i])<=0) top--;
st[++top]=p[i];
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
graham();
double minn=INF;
for(int i=1;i<=top;i++) ans[i]=st[i];
sort(ans+1,ans+top+1,comp);
ans[top+1]=ans[1];
for(int i=1;i<=top;i++)
{
double l=0,r=INF;
while(r-l>1e-6)
{
double mid=(l+r)/2;
double area=0.0;
point tmp;
tmp.x=ans[i].x+mid;
tmp.y=ans[i].y;
for(int j=1;j<=top;j++)
{
double d=dis(tmp,ans[j]);
if(d>mid) continue;
double ang=acos(d/mid);
double s=ang*mid*mid/2.0;
if(cross(ans[i],tmp,ans[j])<0) s=-s;
area+=s;
}
if(area<0) l=mid;
else r=mid;
}
minn=min(minn,l);
}
printf("%.2lf\n",minn);
return 0;
}
```
该代码主要是使用Graham扫描算法求出凸包,再根据二分查找算法求出最小覆盖圆的半径。其中,判断一个点是否在一个圆内部,可以用到向量的叉积。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)