模拟题,详细见注释。
#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;
}
继续加油。