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:
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.
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:
Sample Output:
题目大意:
公共自行车站点的自行车数量是半满的时候称为完美.不完美的站点就需要调度.给定起点和终点,找到最短的一条路线,并把沿途经过的站点全部调度为完美.如果有多条路线存在,选择从起点带出自行车最小的那一条.最后输出从起点带出的自行车数,路线,从终点带回的自行车数.
思路
单源最短路径问题dijkstra
是跑不掉了.问题的关键在于,当松弛的对象v的最短路径预期dist[v]
和当前最短路径于权值的和dist[min]+edge(min,v)
相等时候的取舍.
一个简单的想法是,出现一致时,计算最短路上平均节点的点权,取最大的那一条.很不幸的是,dijkstra
执行过程中并不知道所要比较的两条最短路的长度.
参考了其他人的思路,与其在还未生成最短路径时想办法取舍,不如等到所有的最短路径都求出来以后再去选最优的那一条.原版算法中记录前驱结点的int path[]
就要改成vector<int> pre[]
了.松弛成功时,清空path[i]
,放入min
.权值相同时,放入min
.
|
|
跑完dijkstra
,path[i]
的笛卡尔积就是所有的最短路径.
现在问题转化为,给定好几条路径,如何选出最优的那一条.dfs
帮了大忙.
进入dfs
后二话不说先将当前节点放入temp_path
,在返回之前再将其弹出.这保证了,调用dfs(sp)后
,只要v
是起点,temp_path
就是一条完整的路径(倒序).倒过来就能推出带出和带回的自行车数.
- 当节点的自行车数与完美状态的差值
bicycle[i]=s[i]-cmax/2
为正时,在离开该节点时需要带走这么多车子.back+=bicycle[i]
. - 当
bicycle[i]
为负时,考虑两种情况.back
比bicycle[i]
的绝对值大时,表示携带的自行车比当前节点要多.只需分给该节点|bibycle[i]|
这么多的自行车即可达成完美.back+=bicycle[i]
- 当
back
比bicycle[i]
的绝对值要小时,表示携带的自行车不够分,需要从起点携带.两者的差值就是所需的车辆数.need+=(0-bicycle[i])-back
,携带的车辆数置零back=0
选出need
和back
最小的路线即可.
|
|
一点小想法
- 用
dfs
外加辅助存储可以实现类似笛卡尔积的效果.比排列组合要方便不少 - 经典算法之所以经典的原因在于,很多问题可以用经典算法的变体解决.适用面大,所以经典.
- 在计算
back
和need
时又用到了递推.给递推打个星⭐