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. Flask测试和部署

    一 蓝图Blueprint 为什么学习蓝图? 我们学习Flask框架,是从写单个文件,执行hello world开始的.我们在这单个文件中可以定义路由.视图函数.定义模型等等.但这显然存在一个问题:随 ...

  2. c# .NET RSA结合AES加密服务端和客户端请求数据

    这几天空闲时间就想研究一下加密,环境是web程序,通过js请求后台返回数据,我想做的事js在发送请求前将数据加密,服务端收到后解密,待服务端处理完请求后,将处理结果加密返回给客户端,客户端在解密,于是 ...

  3. asp.net——上传图片生成缩略图

    上传图片生成缩略图,原图和缩略图地址一样的时候缩略图会把原图覆盖掉 /// <summary> /// 生成缩略图 /// </summary> /// <param n ...

  4. IOS渠道追踪方式

    本文来自网易云社区 作者:马军 IOS,安卓渠道追踪的差异 Google Play国内不可用,国内的安卓 App 分发,都是依托几十个不同的应用市场或发行渠道,如百度.360.腾讯等互联网企业以及小米 ...

  5. Cookie的创建与删除

    Cookie 为 Web 应用程序保存用户相关信息提供了一种有用的方法.例如,当用户访问站点时,可以利用 Cookie 保存用户首选项或其他信息,这样,当用户下次再访问站点时,应用程序就可以检索以前保 ...

  6. JSOI2008 Blue Mary开公司 | 李超线段树学习笔记

    题目链接:戳我 这相当于是一个李超线段树的模板qwqwq,题解就不多说了. 代码如下: #include<iostream> #include<cstdio> #include ...

  7. net项目总结一(1)

    中小型新闻发布系统 代码结构:分为实体层,数据层与接口,数据工厂层,业务逻辑层,公共层,UI层(由于图片上传实在麻烦,所以只上传少量而已),项目中用到了工厂模式,解耦BLL层和DLL层 1.登录功能, ...

  8. 前端切图要选择png和jpg呢?

    今天特意验证了一下: 切完图分别保存png24.png8和jpg60.jpg80(60和80表示保存图片时品质选择)后, 然后再压缩图片,压缩图片地址:https://tinypng.com/ 图片直 ...

  9. LOJ#6360. 复燃「恋之埋火」(最小圆覆盖+高斯消元)

    题面 传送门 题解 不难发现最小圆覆盖的随机增量法复杂度还是正确的 所以现在唯一的问题就是给定若干个点如何求一个\(m\)维的圆 其实就是这一题 //minamoto #include<bits ...

  10. git 使用merge 对本地分支进行合并 并进行代码提交的流程

    1.只有当将修改内容commit后 该修改才完全生效,进行merge前需要将两个分支修改的内容都进行commit 2.假设本地两个分支   用于开发的分支:dev    用于同步远程仓库的分支:mas ...