PAT甲级(Advanced Level) Practice 1018 Public Bike Management

2024-03-20 21:12

本文主要是介绍PAT甲级(Advanced Level) Practice 1018 Public Bike Management,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原题

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3​, we have 2 different shortest paths:

  1. PBMC -> S1​ -> S3​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1​ and then take 5 bikes to S3​, so that both stations will be in perfect conditions.

  2. PBMC -> S2​ -> S3​. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax​ (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; Sp​, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci​ (i=1,⋯,N) where each Ci​ is the current number of bikes at Si​ respectively. Then M lines follow, each contains 3 numbers: Si​, Sj​, and Tij​ which describe the time Tij​ taken to move betwen stations Si​ and Sj​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0−>S1​−>⋯−>Sp​. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp​ is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

题目翻译

杭州市的公共自行车服务为世界各地的游客提供了极大的便利。游客可在任意站点租用自行车,并在市内任意站点还车。

公共自行车管理中心(PBMC)对所有站点的容量进行实时监控。如果一个站点正好是半满,则表示该站点处于最佳状态。如果一个站点满了或空了,公共自行车管理中心就会收集或发送自行车,将该站点的状况调整到最佳状态。此外,途中的所有站点也会进行调整。

当收到问题站点的报告时,PBMC 总是会选择最短路径到达该站点。如果有多条最短路径,则会选择需要从 PBMC 发送最少单车的路径。

上图是一个示例。站点由顶点表示,道路则对应于边。边上的数字表示从一个终点站到达另一个终点站所需的时间。写在顶点 S 内的数字是当前存放在 S 的自行车数量。要解决 S3 的问题,我们有 2 条不同的最短路径:

输入规范:

每个输入文件包含一个测试案例。每个案例的第一行包含 4 个数字: Cmax(≤100),总是偶数,是每个站点的最大容量;N(≤500),站点总数;Sp,问题站点的索引(站点编号从 1 到 N,PBMC 由顶点 0 表示);M,道路数量。第二行包含 N 个非负数 Ci(i=1,⋯,N),每个 Ci 分别代表 Si 当前的自行车数量。然后是 M 行,每行包含 3 个数字: Si、Sj 和 Tij,它们描述了在站点 Si 和 Sj 之间移动所需的时间 Tij。一行中的所有数字之间用空格隔开。

输出规范:

对于每个测试案例,请在一行中打印结果。首先输出 PBMC 必须发送的自行车数量。然后在一个空格后,以下列格式输出路径: 0->S1->⋯->Sp。最后,在另一个空格后,输出 Sp 条件调整为完美后,我们必须送回 PBMC 的自行车数量。

注意,如果该路径不是唯一的,则输出我们必须带回 PBMC 的自行车数量最少的路径。法官的数据保证这样的路径是唯一的。

解题思路

我刚看到这道题的想法是用dijkstra求最短路,但是卡在找出带出且带回最少的路径。

在网上找了一下发现大家是用dfs来求的,根据这个思路想了想,发现了dijkstra的一个规律:

if(dist[i] == dist[j] + distance(i, j)), 则i在j的最短路路径上,并且i是j的前一个点。这也是本题dfs的核心。

还有一个难点:怎么计算一条路径带出的和带回的数量。我的方法是记录 dfs到的点(u)身边带着的车的数量 和 带出来的车的数量。这样到达最后一个点时身边剩下的车就是要带回的车数量。

到达新的点时,计算该点需要增加车的数量(可以是负数),如果是正数,则手上的车减少(不够就减则增加带出来的车数量);如果为负数,增加手上车的数量。

这道题的dijkstra就不加注释了,看不懂的可以去看看1003 Emergency

代码(c++)

#include <bits/stdc++.h>
#include <vector>using namespace std;const int N = 510;int c, n, s, m;
int bike[N];
int dist[N], g[N][N];
bool st[N];
int min_bring = 1e9, min_back = 1e9;
vector<int> ans, path;void dijkstra() {memset(dist, 0x3f, sizeof dist);dist[0] = 0;for(int i = 0; i < n; i++) {int t = -1;for(int j = 0; j <= n; j++) if(!st[j] && (t == -1 || dist[j] < dist[t])) t = j;st[t] = true;for(int j = 0; j <= n; j++) dist[j] = min(dist[j], dist[t] + g[t][j]);}
}void dfs(int u, int bring, int have) {if(u == s) {                 // 到达s点,结算// bring是带出来的车辆数,have是手上的车数,刚刚说过结算时就是带走的车数if(bring < min_bring || (bring == min_bring && have < min_back))ans = path, min_bring = bring, min_back = have;return;}for(int i = 0; i <= n; i++) {if(dist[i] == g[u][i] + dist[u]) {          // 按最短路找下一个点int new_have = 0, new_bring = bring;new_have =  have + bike[i] - c / 2;if(new_have < 0) new_bring = bring - new_have, new_have = 0;path.push_back(i);dfs(i, new_bring, new_have);path.pop_back();}}
}int main() {scanf("%d%d%d%d", &c, &n, &s, &m);for(int i = 1; i <= n; i++) scanf("%d", &bike[i]);memset(g, 0x3f, sizeof g);for(int i = 0; i < m; i++) {int a, b, w;scanf("%d%d%d", &a, &b, &w);g[a][b] = min(g[a][b], w);g[b][a] = min(g[b][a], w);}dijkstra();dfs(0, 0, 0);printf("%d 0", min_bring);for(int i = 0; i < ans.size(); ++ i) printf("->%d", ans[i]);printf(" %d\n", min_back);return 0;
}

这篇关于PAT甲级(Advanced Level) Practice 1018 Public Bike Management的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/830734

相关文章

DBeaver 连接 MySQL 报错 Public Key Retrieval is not allowed

DBeaver 连接 MySQL 报错 Public Key Retrieval is not allowed 文章目录 DBeaver 连接 MySQL 报错 Public Key Retrieval is not allowed问题解决办法 问题 使用 DBeaver 连接 MySQL 数据库的时候, 一直报错下面的错误 Public Key Retrieval is

安装SQL2005后SQL Server Management Studio 没有出来的解决方案

一种情况,在安装 sqlServer2005 时 居然出现两个警告: 1 Com+ 目录要求 2 Edition change check 郁闷!网上说出现两个警告,是肯定装不成功的!我抱着侥幸的态度试了下,成功了。 安装成功后,正准备 “ 仅工具、联机丛书和示例(T)” 但是安装不了,他提示我“工作站组件”安装过了对现有组件无法更新或升级。 解决办法: 1 打开“控

SIGMOD-24概览Part7: Industry Session (Graph Data Management)

👇BG3: A Cost Effective and I/O Efficient Graph Database in ByteDance 🏛机构:字节 ➡️领域: Information systems → Data management systemsStorage management 📚摘要:介绍了字节新提出的ByteGraph 3.0(BG3)模型,用来处理大规模图结构数据 背景

MiniCPM-V: A GPT-4V Level MLLM on Your Phone

MiniCPM-V: A GPT-4V Level MLLM on Your Phone 研究背景和动机 现有的MLLM通常需要大量的参数和计算资源,限制了其在实际应用中的范围。大部分MLLM需要部署在高性能云服务器上,这种高成本和高能耗的特点,阻碍了其在移动设备、离线和隐私保护场景中的应用。 文章主要贡献: 提出了MiniCPM-V系列模型,能在移动端设备上部署的MLLM。 性能优越:

POJ 1018 Communication System(枚举)

题目: http://poj.org/problem?id=1018 题解: 我们可以枚举每一种B可能的值,然后寻找每一行里大于等于B里最小的P。 代码: #include<cstdio>#include<stdlib.h>struct in{double B,P;}a[101][101];double b[10001];int t[101];int cmp(cons

c++ public、protected 、 private访问修饰符详解

在 C++ 中,访问修饰符用于控制类的成员(数据成员和成员函数)的访问权限。主要的访问修饰符有三个:public、protected 和 private。每种修饰符的访问规则如下: 1. public 定义:public 修饰符表示该成员对所有代码都是可见的,任何对象都可以访问和修改。作用:允许类外部的代码访问这些成员。 class Example {public:int publicVa

访问修饰符public、protected、private,基于C++

一、基本概念 公有(public)成员   公有成员在程序中类的外部是可访问的。您可以不使用任何成员函数来设置和获取公有变量的值, 私有(private)成员  私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。 默认情况下,类的所有成员都是私有的。例如在下面的类中,width 是一个私有成员,这意味着,如果您没有使用任何访问修饰符,类的成

JAVA反射使用父类的非public方法(getMethods()和getDeclaredMethods()区别)

getMethods()和getDeclaredMethods()区别 虽然是老生常谈了,但是还是要先说一下两者的区别。 getMethods():能够获取类的所有public方法,包括自身定义的以及从父类继承的。 getDeclaredMethods():能够获取类本身的所有方法,包括private方法,实现的接口方法,但是不能获取从父类继承的非public方法。 因此getDeclaredM

PAT甲级-1044 Shopping in Mars

题目   题目大意 一串项链上有n个钻石,输入给出每个钻石的价格。用m元买一个连续的项链子串(子串长度可为1),如果不能恰好花掉m元,就要找到最小的大于m的子串,如果有重复就输出多个,按递增顺序输出子串的前端和后端索引。 原来的思路 取连续的子串使和恰等于m,没有恰等于就找最小的大于。可以将子串依次累加,使得每个位置都是起始位置到该位置的序列和,整个数组显递增顺序,就可以用右边减左边

【SqlServer】SQL Server Management Studio (SSMS) 下载、安装、配置使用及卸载——保姆级教程

超详细的 SQL Server Management Studio (SSMS) 下载、安装、连接数据库配置及卸载教程 SQL Server Management Studio (SSMS) 是微软提供的图形化管理工具,主要用于连接、管理和开发 SQL Server 数据库。以下是详细的 SSMS 下载、安装、连接数据库以及卸载的完整教程。 一、SSMS 下载与安装 1.1 下载 SSM