HDU 1571(下沙小面的(1))

本文介绍了一个基于C++实现的公交调度模拟程序。该程序通过处理乘客上下车请求和计算最优行驶路径,模拟了公交车在不同站点间的运行情况。程序考虑了乘客数量限制和目的地,以及车辆在各站点之间的行驶距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟题,详细见注释。

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 35;
const int MAXP = 10;
const int INF = 0x3f3f3f3f;

int dis[MAXN][MAXN]; //车站间的距离

struct Passenger //乘客
{
	int to; //目的车站
	int num; //上车次序
}p[MAXP];

int main()
{
	int N;
	while (scanf("%d", &N))
	{
		if (N == 0)
			break;

		memset(dis, 0, sizeof(dis));
		for (int i = 0; i < MAXP; i++)
		{
			p[i].to = -1;
			p[i].num = INF;
		}

		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < N; j++)
				scanf("%d", &dis[i][j]);
		}

		int K; //命令数
		char command[5]; //命令
		int total = 0; //行驶总距离
		int count = 0; //上车总人数
		int nowS = 0; //当前所在车站
		int nowP = 0; //车上当前人数
		scanf("%d", &K);
		while (K--)
		{
			scanf("%s", command);
			if (command[0] == 'U') // UP
			{
				int to; //当前要上车的乘客的到达车站
				scanf("%d", &to);
				if (nowS != to && nowP != 7)
				{
					++nowP;
					++count;
					for (int i = 1; i <= 7; i++)
					{
						if (p[i].to == -1)
						{
							p[i].to = to;
							p[i].num = count;
							break;
						}
					}
				}
			}
			else // GO
			{
				if (nowP != 0)
				{
					int minNum = p[1].num; //车内乘客最早上车次序
					int to = p[1].to; //到达车站
					for (int i = 2; i <= 7; i++) //查找最早上车乘客
					{
						if (p[i].num < minNum)
						{
							minNum = p[i].num;
							to = p[i].to;
						}
					}
					total += dis[nowS][to];
					nowS = to;
					for (int i = 1; i <= 7; i++) //乘客下车
					{
						if (p[i].to == to)
						{
							--nowP;
							p[i].num = INF;
							p[i].to = -1;
						}
					}
				}
			}
		}
		printf("%d\n", total);
	}
	return 0;
}

继续加油。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值