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 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​​ (≤100), always an even number, is the maximum capacity of each station; N(≤500), 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​​ (i=1,⋯,N) 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−>S​1​​−>⋯−>S​p​​. 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.

思路:

Dijkstra求出所有最短路 + DFS求出min need的路

在DFS中回溯的时候,注意:从起点(PBMC)开始走,先前的back可以抵消后面的need,但是后面的back不能抵消先前的need

前面的need也不影响后面的back;

也就是说,每个点的不足,由PBMC或者其先前的站点补足,与下游站点无关

 #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = ;
const int maxn = ; int cmax,n,sp,m;
int dismap[maxn][maxn];
int w[maxn],dis[maxn];
vector<int> pre[maxn];
vector<int> path,tmppath;
int minneed=inf ,minback=inf; void dijkstra(int s){
int vis[maxn]; fill(vis,vis+maxn,);
fill(dis,dis+maxn,inf);
dis[s]=;
for(int i=;i<=n;i++){
int minv,mind=inf;
for(int v=;v<=n;v++){
if(!vis[v]&&dis[v]<mind){
mind=dis[v];
minv=v;
}
}
if(mind==inf) break;
vis[minv]=;
for(int v=;v<=n;v++){
if(!vis[v]&&dismap[minv][v]!=inf){
if(dis[v]>mind+dismap[minv][v]){
dis[v]=mind+dismap[minv][v];
//printf("# %d %d %d\n",dis[v],minv,v);
pre[v].clear();
pre[v].push_back(minv);
}else if(dis[v]==mind+dismap[minv][v]){
pre[v].push_back(minv);
}
}
}
}
} void dfs(int v){
tmppath.push_back(v);
if(v==){
int back=,need=;
for(int i=tmppath.size()-;i>=;i--){
int id=tmppath[i]; if(w[id]>=){
back+=w[id]; }else{
if(-w[id]<back){
back+=w[id];
}else{
need+=(-w[id])-back;
back=;
//printf("# %d %d %d\n",id,w[id],need);
}
}
}
if(need<minneed){
path=tmppath;
minneed=need;
minback=back;
}else if(need==minneed&&back<minback){
path=tmppath;
minback=back;
}
tmppath.pop_back();
return;
}
for(int i=;i<pre[v].size();i++){
dfs(pre[v][i]);
}
tmppath.pop_back();
} int main(){
fill(dismap[],dismap[]+maxn*maxn,inf); scanf("%d %d %d %d",&cmax,&n,&sp,&m);
for(int i=;i<=n;i++){
scanf("%d",&w[i]);
w[i]-=cmax/;
}
int a,b,tmpd;
for(int i=;i<m;i++){
scanf("%d %d %d",&a,&b,&tmpd);
dismap[a][b]=dismap[b][a]=tmpd;
}
//printf("# %d\n",w[3]);
dijkstra();
dfs(sp);
printf("%d ",minneed);
for(int i=path.size()-;i>=;i--){
printf("%d",path[i]);
if(i!=) printf("->");
}
printf(" %d",minback);
}

1018 Public Bike Management的更多相关文章

  1. PAT 1018 Public Bike Management[难]

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

  2. PAT甲级1018. Public Bike Management

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

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

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

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

  5. 1018. Public Bike Management (30)

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

  6. PAT 1018. Public Bike Management

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

  7. PTA (Advanced Level) 1018 Public Bike Management

    Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...

  8. 1018 Public Bike Management (30)(30 分)

    时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...

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

随机推荐

  1. k8s学习笔记之六:Pod控制器(kube-controller-manager)

    第一章.什么是kube-controller-manager? Controller Manager 由 kube-controller-manager 和 cloud-controller-mana ...

  2. scrapy-pipeline的方法

    scrapy中多个pipeline作用: 一个项目可能需要爬取多个网站,根据每个网站的数据量(处理方式)不同,可创建多个管道 pipeline class SpideranythingPipeline ...

  3. CentOS6.8 下RPM方式安装MySQL5.6

    1. 检查MySQL及相关RPM包,是否安装,如果有安装,则移除(rpm –e 名称) yum remove mysql mysql-server mysql-libs(我用的上面的)或者 [root ...

  4. node.js获取本机Ip, hostName, mac

    //获取ip地址 getIPAdress() { let interfaces = require('os').networkInterfaces(); for (var devName in int ...

  5. sass的基本使用

    使用sass的前提是安装Ruby,如果是Mac系统,那么免去安装,Windows系统需要自行安装https://www.sass.hk/install/.当安装好以后,直接执行安装sass命令:gem ...

  6. 部署一个基于python语言的web发布环境

    ---恢复内容开始--- 1) 一门面向对象的语言 2)拥有丰富的库 3)可移植性 4)免费.开源 5)简单易易学 可做软件开发.人工智能.web开发等等 部署流程: Cnetos7.5+Nginx+ ...

  7. TCC细读 - 3 恢复流程

    重试定时任务,通过外部调度实现 package org.mengyun.tcctransaction.spring.recover; import org.mengyun.tcctransaction ...

  8. ffmpeg使用经验

    1.工作要使用ffmpeg将视频转换成H264格式,网上查到的很多使用方法都是如下: ffmpeg -i input.mov -c:v libx264 -crf output.mov -i后面表示输入 ...

  9. The history of programming languages.(transshipment) + Personal understanding and prediction

    To finish this week's homework that introduce the history of programming languages , I surf the inte ...

  10. zabbix钉钉报警

    我们在钉钉上建立群聊,然后在群聊上添加钉钉机器人: 编写,脚本需要放在zabbix 的alertscripts目录下(如果不知道该目录的位置,可以使用find命令查找) find / -iname a ...