C++的救护车调度系统

C++的救护车调度系统,很详细!

一、问题描述

某救护中心所在服务区有5辆救护车,当需要调度时,需要判定哪辆救护车是最优的,不同车辆的油箱容量(单位为公升)、燃油效率(单位为公里/公升)不同。
判定条件为:

(一)救护车中的燃料能够使救护车从当前位置行驶到待救护点,然后再行驶到救护中心;

(二)救护车距离待救护点最近(距离根据两点之间的距离公式计算,单位为公里);

(三)根据油箱中剩余的燃油量和燃油效率能够计算出救护车还能够行驶的里程数。

二、样例输入和样例输出

第1行输入救护中心的位置坐标(x y),用空格隔开;

第2~6行分别输入第1~5辆救护车的位置坐标(x y)、油箱容积、现有燃油量、燃油效率;

第7行输入待救护点的位置坐标(x y),用空格隔开;

如果能找到最优的救护车,则输出救护车的序号(从0开始)和其能够行驶的最大里程信息;

如果找不到满足条件的救护车,则输出“Scheduling Failure!”。

(一)样例输入

197.784 161.559

166.125 190.908 108 39.76 6.85

131.267 118.553 118 85.77 5.78

170.888 147.168 104 0.25 5.54

167.075 121.17 118 57.12 6.92

194.047 114.992 100 88.91 5.72

160.165 152.769

(二)样例输出

the Ambulance index = 0;the max Mileage = 272.356

三、代码实现

#include <bits/stdc++.h>

using namespace std;

#define AMNUMS 5
#define MAX_FLOAT 3.40282325E38
class point2d{
public:
	point2d(){};
	point2d(double x, double y){
		_x = x;
		_y = y;
	}
	double getX() const{
		return _x;
	}
	double getY() const{
		return _y;
	}
	void setx(double x){
		_x = x;
	}
	void sety(double y){
		_y = y;
	}
private:
	double _x = 0.0;
	double _y = 0.0;
};

double getDistance(const point2d & pt1, const point2d & pt2){
	double dx = pt1.getX() - pt2.getX();
	double dy = pt1.getY() - pt2.getY();
	return sqrt(dx * dx + dy * dy);
}

class Ambulance_car{
private:
	double m_lfCapcity;
	double m_lfRemain;
	double m_lfRatio;
	point2d m_curPosition;
public:
	Ambulance_car(){};
	void setcap(double cap){
		m_lfCapcity = cap;
	}
	void setrem(double rem){
		m_lfRemain = rem;
	}
	void setrat(double rat){
		m_lfRatio = rat;
	}

	void setPosition(point2d p){
		m_curPosition.setx(p.getX());
		m_curPosition.sety(p.getY());
	}
	double getcap(){
		return m_lfCapcity;
	}
	double getrem(){
		return m_lfRemain;
	}
	double getrat(){
		return m_lfRatio;
	}
	double getmaxx(){
		return getrat() * getrem();
	}
	point2d getpos(){
		return m_curPosition;
	}
};

class EmergencyCenter{
private:
	point2d m_CenterPos;
	Ambulance_car m_arCar[AMNUMS];
public:
	EmergencyCenter(){};
	void setPosition(point2d p){
		m_CenterPos.setx(p.getX());
		m_CenterPos.sety(p.getY());
	}
	void setAmPosition(int i, point2d p){
		m_arCar[i].setPosition(p);
	}
	void setAmStatus(int i, double cap, double rem, double rat){
		m_arCar[i].setcap(cap);
		m_arCar[i].setrem(rem);
		m_arCar[i].setrat(rat);
	}
	double getAmmaxMileage(int i){
		return m_arCar[i].getmaxx();
	}
	int scheduling(const point2d & ptResecue){
		int id = -1;
		double max_dis = 1e18;
		for(int i = 0; i < AMNUMS; i++){
			double disa = getDistance(m_arCar[i].getpos(), ptResecue);
			double disb = getDistance(m_CenterPos, ptResecue);
			double max_meter = getAmmaxMileage(i);
			if(max_meter >= disa + disb){
				if(max_meter < max_dis){
					id = i;
					max_dis = max_meter;
				}
			}
		}
		return id;
	}
};

int main()
{
    double Ifx,Ify;
    Ifx = Ify = 0.0;
    cin>>Ifx>>Ify;
    point2d pttemp(Ifx,Ify);
    EmergencyCenter emcenter;
    emcenter.setPosition(pttemp);


    double Ifcap,Ifrem,Ifrat;
    for(int i=0;i<AMNUMS;++i)
    {
        cin>>Ifx>>Ify>>Ifcap>>Ifrem>>Ifrat;

        pttemp.setx(Ifx);
        pttemp.sety(Ify);
        emcenter.setAmPosition(i,pttemp);

        emcenter.setAmStatus(i,Ifcap,Ifrem,Ifrat);
    }
    cin>>Ifx>>Ify;
    pttemp.setx(Ifx);
    pttemp.sety(Ify);

    int nidx=emcenter.scheduling(pttemp);

    cout.flags(ios::fixed);
    cout.precision(3);
    if(nidx==-1)
    {
        cout<<"Scheduling Failure!"<<endl;
    }
    else
    {
        cout<<"the Ambulance index="<<nidx
            <<";the max Mileage="<<emcenter.getAmmaxMileage(nidx)<<endl;
    }
    return 0;
}

四、程序运行结果

猜你喜欢

转载自blog.csdn.net/KK_2018/article/details/128529284