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. 剑指offer题目解答合集(C++版)

    数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...

  2. shell脚本四-三剑客

    Shell编程——三剑客 简介 Grep:默认不支持扩展表达式,加-E或者egrep Awk:支持所有zhengze Sed默认不支持扩展表达式,加-r 2.sed语法格式 Sed 选项 命令 文件( ...

  3. 【待考察】Appium使用技巧,助你快速入门移动端自动化!

    Appium使用技巧,助你快速入门移动端自动化! 原创: 柠檬班superman 柠檬班软件测试 1月4日 关注并置顶[柠檬班]的小哥哥小姐姐 “猪”年行大运 说说最近研究移动端的自动化 移动端的自动 ...

  4. Fiddler手机抓包软件简单使用--将h5效果显示在手机

    此文章是博主自己所写,转载请注明出处 一.简介 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据. ...

  5. 205. jetcache:你需要知道的小技巧

    [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...

  6. 深入理解Servlet原理

    Servlet介绍: 1. 如何返回一个网页: a. 静态网页 >新闻页.百度百科 >服务器保存一份HTML,直接返回给浏览器即可 b. 动态网页 >淘宝.QQ空间.微博 >服 ...

  7. 怎样让scrollview滚动到底部?

    - (void)scrollsToBottomAnimated:(BOOL)animated { CGFloat offset = self.tableView.contentSize.height ...

  8. spring boot 之 spring security 配置

    Spring Security简介 之前项目都是用shiro,但是时过境迁,spring security变得越来越流行.spring security的前身是Acegi, acegi 我也玩过,那都 ...

  9. tesseract_vs2015工具包使用

    在vs中配置tesseract4.0: 新建一个空的控制台程序.并找到下图位置 双击.x64.user出现如下图: 单击VC++目录,将tesseract-2015/include/tesseract ...

  10. Unity for VsCode

    安装以下两个插件 以下设置VsCode在换行保存时不删除tab空格