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. codeblock用法

    1.链接动态库.so和静态库.a settings->compiler->linker settings->add 2.编译报错ld return 1 exit status 一般来 ...

  2. java 栈 最大深度

      1. 概述 某公司面试,总监大叔过来,问了图论及栈的最大深度,然后^_^ 一直记着,今天搞一下 2. 代码 package com.goodfan.test; public class JavaS ...

  3. Android 开发 框架系列 glide-transformations 图片处理基本使用

    首先简单的介绍一下Gilde作用范围.Gilde功能十分强大,它可以实现图片处理.图片本地加载.图片网络加载.位图加载.图片内存缓存.图片磁盘缓存.Gif图片加载.使用简单轻松,轻松的后是它强大的心, ...

  4. 61.纯 CSS 创作一只咖啡壶(这个不好看)

    原文地址:https://segmentfault.com/a/1190000015376202 感想: 好像不像呀,啊啊啊.伪元素.定位.动画.width和height包括内边距|边框|内容区. H ...

  5. 3.1链表----链表(Linked List)入门

    在分析链表之前,我们先来对之前的动态数组.栈.队列总结一下: (1)底层依托于静态数组 (2)依靠resize解决固定容量问题 (3)是一种假的的动态数据结构 1.什么是链表 可以从以下两个部分来理解 ...

  6. 阿里云RDS读写分离数据查询延迟解决

    mysql使用RDS做数据主从读写分离.在使用的过程中发现部分业务对其他服务以来严重.但是由于系统不是采用微服务的架构,造成部分数据插入数据库后,后续操作读取数据库没有查询到前面插入的数据.查看阿里云 ...

  7. Delphi中Chrome Chromium、Cef3学习笔记(一)

    原文   http://blog.csdn.net/xtfnpgy/article/details/46635225   官方下载地址:https://cefbuilds.com/ CEF简介: 嵌入 ...

  8. GDI+_入门教程【一】

    GDI For VisualBasic6.0 [一]文件下载:GDI+ For VB6[一] 简单绘图实例演示百度网盘 1 '以下为作者[vIsiaswx]的教程 '(该教程发布的原地址已无法访问,此 ...

  9. 微信小程序--预览previewImage(长按保存图片)

    最近开发小程序,想实现二维码图片长按保存,发现无法保存,只能让图片先预览,再保存.注意:只有太阳码才有长按保存和识别功能,普通二维码只有长按保存功能. <image class='banner' ...

  10. react-native 新手爬坑经历(unable to load script from assets 和could not connect to development server.)

    按照https://reactnative.cn/docs/0.51/getting-started.html教程新建的项目 react-native init AwesomeProject cd A ...