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 perfectcondition 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 S​3​​, we have 2 different shortest paths:

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

  2. PBMC -> S​2​​ -> S​3​​. 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: C​max​​ (≤), always an even number, is the maximum capacity of each station; N (≤), the total number of stations; S​p​​, 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 C​i​​ (,) where each C​i​​ is the current number of bikes at S​i​​ respectively. Then Mlines follow, each contains 3 numbers: S​i​​, S​j​​, and T​ij​​ which describe the time T​ij​​ taken to move betwen stations S​i​​ and S​j​​. 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. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S​p​​ 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

解题思路:
  本题题意是,杭州市有一些共享单车站,给出车站的最大容量(一定是偶数),给出车站数量与车站之间道路的信息,车站的最优状态为最大容量的一半。给定目标车站,控制中心现在要携带或从路过的其他车站取出一些自行车,使路程中路过的所有车站和目标车站都变为最优状态,多的自行车将被带回控制中心。要求计算并输出最短路径和最短路径情况下的携带数与带回数,如果有多条最短路径那么选择携带数最少的一条,如果还有是多条路径,那么选择带回数最少的一条。(到达目标车站后之间带着多余的自行车之间传送回控制中心,不在经过任何车站)。

  输入信息:第一行给出4个整数,分别为 最大容量Cmax ,车站数量n, 目标车站sp,道路数量m,下一行给出n个整数代表每个车站当前自行车数量,之后跟随m行,每行3个整数,分别为,道路连接的两个车站和道路长度。

  车站的范围为1 ~ n,0为控制中心。

  可以先用迪杰斯特拉获得所有最短路径。之后搜索所有最短路径获得携带数(携带数相同获得最小的带回数)最小的路径,按要求输出。

  利用Dijkstra建立一颗最短路径树

void Dijkstra(int st){  //传入起点,这里之间传入0控制中心即可
dis[st] = ; //dis储存控制中心到所有车站的最短距离
//控制中心到自己的最短距离为0
for(int i = ; i <= n; i++){ //获取没有访问过的车站中距离控制中心最近的车站
int u = -, minLength = INT_MAX;
//u记录最近的车站, minLength记录最短距离
for(int j = ; j <= n; j++){
//vis记录车站是否已经访问
if(dis[j] < minLength && !vis[j]){
u = j;
minLength = dis[j];
}
}
if(u == -) //如果u还是-1表示其他车站和控制中心不连通
return;
vis[u] = true; //将最近的车站标记为已访问
for(int j = ; j <= n; j++){ //遍历所有结点用u车站优化控制中心与其他未访问车站的距离
if(!vis[j] && G[u][j] != INT_MAX){ //j车站未访问且与u车站之间有道路
if(dis[j] > dis[u] + G[u][j]){ //判断是否能优化
dis[j] = dis[u] + G[u][j]; //如果能优化
pre[j].clear(); //j车站的前驱车站清空并重新记录为u
pre[j].push_back(u);
}else if(dis[j] == dis[u] + G[u][j]){
//若不能优化但是以u为中转是j车站到控制中心的距离和dis数组中记录的 j车站到控制中心的距离相等
//j车站的前驱增加u
pre[j].push_back(u);
}
}
}
}
}

  之后深搜遍历最短路径树获得携带数最少(如果仍有多条道路则获得带回数最少)的路线。

void DFS(int v){
if(v == ){ //这里我们逆序遍历路径,从终点车站开始到控制中心0为止
//应为最短路径树中记录控制0为叶子结点,目标车站sp为根结点
path.push_back();
judge(); //判断当前路径的携带数与带回树是否可以优化答案
path.pop_back();
return;
}
path.push_back(v);
for(auto i : pre[v]){
DFS(i);
}
path.pop_back();
}

  由于DFS中我们时倒序遍历的最短路径,所以在判断每条路径是否可以优化答案时,要先将DFS中获取的路径反转,之后遍历这条路径获得其携带数与带回数即可。

void judge(){
int need = , backn = ; //need记录携带数 backn记录带回数
int half = Cmax / ; //half记录最优状态车站中车辆数
reverse(path.begin(), path.end());
bool flag = false; //道路开始位置为控制中心0,我们不需要将控制中心的车辆数调整为最优状态
//所以我们用flag = false表示正在访问控制中心
for(auto i : path){
if(!flag){ //跳过控制中心
flag = true;
continue;
}
if(spValue[i] > half){ //当前车站中的共享单车数量大于最优状态数量
backn += spValue[i] - half; //将多出的车辆带走
}else{
if(backn > half - spValue[i]) //如果当前携带的车辆足以将该车站补充为最优状态
backn -= half - spValue[i]; //当前携带数减去缺少车辆数
else{
need += (half - spValue[i]) - backn;
//如果不足以补足当前车站就将所有携带车辆放入该车站,之后还是不足的车辆从控制中心携带
backn = ;
}
}
}
if(need < minNeed){ //比较答案与当前路径的携带数
minNeed = need;
minBack = backn;
ansPath = path;
}else if(need == minNeed && backn < minBack){ //携带数一致比较带回数
minBack = backn;
ansPath = path;
}
reverse(path.begin(), path.end()); //为了继续DFS重新将路径反转
}

  AC代码

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int n, m, Cmax, sp; //n记录车站数量, m记录道路数量, Cmax记录车站最大容量(Cmax/2就是车站最优数量)
int G[maxn][maxn]; //G储存车站之间的邻接矩阵
int spValue[maxn], dis[maxn]; //spValue记录每个车站当前的车辆数, dis记录每个车站到控制中心的最短距离
bool vis[maxn] = {false}; //vis用于在寻找最短路径时判断车站是否已经访问
int minNeed, minBack;
vector<int> path, ansPath, pre[maxn];
void Dijkstra(int st){ //传入起点,这里之间传入0控制中心即可
dis[st] = ; //dis储存控制中心到所有车站的最短距离
//控制中心到自己的最短距离为0
for(int i = ; i <= n; i++){ //获取没有访问过的车站中距离控制中心最近的车站
int u = -, minLength = INT_MAX;
//u记录最近的车站, minLength记录最短距离
for(int j = ; j <= n; j++){
//vis记录车站是否已经访问
if(dis[j] < minLength && !vis[j]){
u = j;
minLength = dis[j];
}
}
if(u == -) //如果u还是-1表示其他车站和控制中心不连通
return;
vis[u] = true; //将最近的车站标记为已访问
for(int j = ; j <= n; j++){ //遍历所有结点用u车站优化控制中心与其他未访问车站的距离
if(!vis[j] && G[u][j] != INT_MAX){ //j车站未访问且与u车站之间有道路
if(dis[j] > dis[u] + G[u][j]){ //判断是否能优化
dis[j] = dis[u] + G[u][j]; //如果能优化
pre[j].clear(); //j车站的前驱车站清空并重新记录为u
pre[j].push_back(u);
}else if(dis[j] == dis[u] + G[u][j]){
//若不能优化但是以u为中转是j车站到控制中心的距离和dis数组中记录的 j车站到控制中心的距离相等
//j车站的前驱增加u
pre[j].push_back(u);
}
}
}
}
}
void judge(){
int need = , backn = ; //need记录携带数 backn记录带回数
int half = Cmax / ; //half记录最优状态车站中车辆数
reverse(path.begin(), path.end());
bool flag = false; //道路开始位置为控制中心0,我们不需要将控制中心的车辆数调整为最优状态
//所以我们用flag = false表示正在访问控制中心
for(auto i : path){
if(!flag){ //跳过控制中心
flag = true;
continue;
}
if(spValue[i] > half){ //当前车站中的共享单车数量大于最优状态数量
backn += spValue[i] - half; //将多出的车辆带走
}else{
if(backn > half - spValue[i]) //如果当前携带的车辆足以将该车站补充为最优状态
backn -= half - spValue[i]; //当前携带数减去缺少车辆数
else{
need += (half - spValue[i]) - backn;
//如果不足以补足当前车站就将所有携带车辆放入该车站,之后还是不足的车辆从控制中心携带
backn = ;
}
}
}
if(need < minNeed){ //比较答案与当前路径的携带数
minNeed = need;
minBack = backn;
ansPath = path;
}else if(need == minNeed && backn < minBack){ //携带数一致比较带回数
minBack = backn;
ansPath = path;
}
reverse(path.begin(), path.end()); //为了继续DFS重新将路径反转
}
void DFS(int v){
if(v == ){ //这里我们逆序遍历路径,从终点车站开始到控制中心0为止
//应为最短路径树中记录控制0为叶子结点,目标车站sp为根结点
path.push_back();
judge(); //判断当前路径的携带数与带回树是否可以优化答案
path.pop_back();
return;
}
path.push_back(v);
for(auto i : pre[v]){
DFS(i);
}
path.pop_back();
}
int main()
{
while(scanf("%d%d%d%d", &Cmax, &n, &sp, &m) != EOF){ //输入最大容量 车站数量 目标车站 道路数量
fill(G[], G[] + maxn * maxn, INT_MAX); //将所有车站之间设为不可达
for(int i = ; i < maxn; i++){
pre[i].clear(); //清空最短路径树
}
path.clear(); //清空用来记录每个路径的容器
ansPath.clear(); //清空用来记录答案路径的容器
for(int i = ; i <= n; i++){
scanf("%d", &spValue[i]); //输入每个车站当前的车辆数
}
for(int i = ; i < m; i++){ //输入路径
int u, v;
scanf("%d%d", &u, &v);
scanf("%d", &G[u][v]);
G[v][u] = G[u][v]; //双向路径
}
memset(vis, false, sizeof(vis)); //将所有车站设为未访问
fill(dis, dis + maxn, INT_MAX); //所有车站到控制中心的距离为无穷大
Dijkstra(); //迪杰斯特拉获取最短路径树
minNeed = INT_MAX, minBack = INT_MAX; //记录答案携带数与带回数为无穷多
DFS(sp); //深搜最短路径树获取答案
printf("%d ", minNeed); //输出携带数
bool flag = false;
for(auto i : ansPath){ //输出路径
if(flag)
printf("->");
printf("%d", i);
flag = true;
}
printf(" %d\n", minBack); //输出带回数
}
return ;
}

PTA (Advanced Level) 1018 Public Bike Management的更多相关文章

  1. PAT (Advanced Level) 1018. Public Bike Management (30)

    先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include&l ...

  2. Pat(Advanced Level)Practice--1018(Public Bike Management)

    Pat1018代码 题目描写叙述: There is a public bike service in Hangzhou City which provides great convenience t ...

  3. PAT 1018 Public Bike Management[难]

    链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018  Public ...

  4. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

  5. PAT 1018 Public Bike Management(Dijkstra 最短路)

    1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. 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 ...

  7. PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  8. 1018. Public Bike Management (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...

  9. PAT 1018. Public Bike Management

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

随机推荐

  1. Oracle数据泵导出导入(expdp/impdp)

    一.创建表空间 create tablespace atp logging datafile 'D:\oracle\oradata\orcl\atp.dbf' size 50m autoextend ...

  2. C#中datagridviewz中SelectionMode的四个属性的含义

    C#中datagridviewz中SelectionMode的四个属性的含义 DataGridViewSelectionMode.ColumnHeaderSelect 单击列头就可以选择整列DataG ...

  3. Js 事件详解

    1.事件流 1.1 事件流 描述的是在页面中接受事件的顺序 1.2 事件冒泡 由最具体的元素接收,然后逐级向上传播最不具体的元素的节点(文档) 1.3 事件捕获 最不具体的节点先接收事件,而最具体的节 ...

  4. Modular Arithmetic ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Introduction Modular arithmetic is a fundamental tool in modern algebra systems. In conjunction wi ...

  5. bat windows10系统垃圾清理---

    @echo off color 0a title windows10系统垃圾清理--- echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★☆ ★☆ ★☆★ echo ★☆ ★☆ ★☆ ★☆ ★☆ ...

  6. WPF TextCompositionManager 事件说明

    TextCompositionManager中三个隧道事件,三个冒泡事件. 除了引发的过程不一样之外其作用是一样的. 事件分别为: InputStart InputUpdate TextInput 其 ...

  7. KindEditor的使用

    KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(textarea)替换为可视化的富文本 ...

  8. 干货 | 精选《SQL注入、渗透、反病毒》学习总结集锦给你们~

    学到手的都是本事,如果觉得对你有帮助也欢迎分享给身边的基友们吧! 分享干货,手留余香哦~ 本次“开学季拜师活动”的徒弟们在师父的精心指导下,在短短5天内得到了迅速地成长,以前或当时遇到的问题都能够柳暗 ...

  9. ionic 项目 随笔

    1,首先 会进入src/index.html, <!-- The polyfills js is generated during the build process --> <sc ...

  10. su: Authentication failure 的解决方案

    原因是:ubuntu默认不允许使用root登录,因此初始root账户是不能使用的,需要在普通账户下利用sudo权限修改root密码. 解决方案很简单:设置一个root密码就行了.注意是sudo 而不是 ...