pat 甲级 Public Bike Management
Public Bike Management (30)
题目描述
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.
Figure 1
Figure 1 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.
输入描述:
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.
输出描述:
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.
输入例子:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
输出例子:
3 0->2->3 0 题意:感觉题意叙述的是真够混乱的,题目描述中说公司派出的车辆越少越好,输出描述中又说回收的车辆越少越好,看得一头雾水,简而言之,若有多条最短路径,筛选出一条最合适的路径。筛选的规则遵循两点,1:公司派出的车辆数目越少越好 2:在公司派出车辆尽量少的情况下,公司需要回收的车辆
数目也尽量的少。至于perfect状态,指一个车站车辆数目正好是车站能够容纳的车辆数目的一半,多了要回收,少了要添加。
思路:最短路,最后求路径可以dfs搜索并筛选。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
#define N_MAX 500+5
#define INF 0x3f3f3f3f
typedef long long ll;
int c_max, n, m, Index;
int c[N_MAX];
struct edge {
int to, cost;
edge() {}
edge(int to,int cost):to(to),cost(cost) {}
};
struct P{
int first, second;
P() {}
P(int first,int second):first(first),second(second) {}
bool operator < (const P&b) const{
return first > b.first;
}
};
vector<edge>G[N_MAX];
int d[N_MAX];
vector<int>Prev[N_MAX];
int V;
void dijkstra(int s) {
priority_queue<P>que;
fill(d, d + V, INF);
d[s] = ;
que.push(P(, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (p.first > d[v])continue;
for (int i = ; i < G[v].size();i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
Prev[e.to].clear();
Prev[e.to].push_back(v);
}
else if (d[e.to] == d[v] + e.cost) {
Prev[e.to].push_back(v);
}
}
}
}
vector<int>r;
int road[N_MAX],process[N_MAX];
int min_num_go = INF,min_num_back=INF;
void dfs(int x,int step,int num) {
road[step] = x;
process[step] = num;
if (x == ) {//前驱结点为起点,搜索终止
int num_go = , num_back;
int tmp_num=;
for (int i = step-; i >= ; i--) {
tmp_num += process[i];
if (tmp_num< && num_go > tmp_num) {//派出车辆必须保证经过的每个站都能达到perfect状态
num_go = tmp_num;
}
}
num_go = -num_go;
num_back = num_go+tmp_num;//假若派出的车辆为0,回收的车辆的数目为tmp_num,实际派出车辆为num_go,则实际回收车辆为num_go+tmp_num
if (min_num_go > num_go) {
min_num_go = num_go;
min_num_back = num_back;
r.clear();
for (int i = step; i >= ; i--)r.push_back(road[i]);
}
else if (min_num_go == num_go&&min_num_back > num_back) {
min_num_back = num_back;
r.clear();
for (int i = step; i >= ; i--)r.push_back(road[i]);
}
return;
}
for (int i = ; i < Prev[x].size();i++) {
int from = Prev[x][i];
if (from)dfs(from, step + , (-c_max / + c[from]));
else dfs(from, step + , num );
}
} int main() {
while (scanf("%d%d%d%d", &c_max, &n, &Index, &m) != EOF) {
V = n + ;
for (int i = ; i <= n; i++)scanf("%d", &c[i]);
for (int i = ; i < m; i++) {
int from, to, cost;
scanf("%d%d%d", &from, &to, &cost);
G[from].push_back(edge(to, cost));
G[to].push_back(edge(from, cost));
}
dijkstra();
dfs(Index, , -c_max / + c[Index]);
cout << min_num_go << " ";
for (int i = ; i < r.size();i++) {
printf("%d%s", r[i], i + == r.size()? " ":"->");
}
cout << min_num_back << endl;
}
return ;
}
pat 甲级 Public Bike Management的更多相关文章
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PAT 1018. Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- [PAT] A1018 Public Bike Management
[思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...
- PAT_A1018#Public Bike Management
Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- 【PAT甲级】Public Bike Management 题解
题目描述 There is a public bike service in Hangzhou City which provides great convenience to the tourist ...
- PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides ...
随机推荐
- vue.js 一(基础环境搭建)
之前由于看了React的东西,写了两篇React的脚手架搭建,一是给自己记一点可用的东西,免得每次都去找,毕竟搭建环境在项目相对固定的时期不是经常要干的事情,一段时间不用就会忘记了. 前端的js框架还 ...
- Redis学习笔记(三)
一.数据备份与恢复 数据备份: localhost:> save OK 该命令会在redis的安装目录中创建文件dump.rdb,并把数据保存在该文件中 查看redis的安装目录: localh ...
- Python基础:输入与输出(I/O)
来做一个NLP任务 步骤为: 1.读取文件: 2.去除所有标点符号和换行符,并把所有大写变成小写: 3.合并相同的词,统计每个词出现的频率,并按照词频从大到小排序: 4.将结果按行输出到文件 out. ...
- Redis实现之对象(二)
列表对象 列表对象的编码可以是ziplist或者linkedlist,ziplist编码的列表对象使用压缩列表作为底层实现,每个压缩列表节点(entry)保存了一个列表元素.举个栗子,如果我们执行RP ...
- Django之session验证的三种姿势
一.什么是session session是保存在服务端的键值对,Django默认支持Session,并且默认是将Session数据存储在数据库中,即:django_session 表中. 二.FVB中 ...
- 手机APP设计网
http://hao.xueui.cn/ http://www.25xt.com/
- 高亮T4模板
http://t4-editor.tangible-engineering.com/Download_T4Editor_Plus_ModelingTools.html
- C语言编程题002
给出两个整数,L和R,其中L<=A<=B<=R,然后求出A^B值最大的数.其中1<=L<=R<=1000. 比如说L = 1;R = 3; L 0001 R 001 ...
- SQL 基础语法详解
SQL 命令一般分为 DQL.DML.DDL DQL:数据查询语句,基本就是 SELECT 查询命令,用于数据查询 DML:Data Manipulation Language 的简称,即数据操纵语言 ...
- Spring Cloud Eureka简单入门
步骤: 1.创建父工程 2.创建EurekaServer工程 3.创建EurekaClient工程 父工程pom.xml <?xml version="1.0" encodi ...