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.


Figure 1

Figure 1 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:

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

2. 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:

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
 #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[][], v[];
int visit[], dst[];
vector<int> pre[];
const int INF = ;
int Cmax, N, Sp, M;
void dijkstra(int s){
fill(visit, visit + , );
fill(dst, dst + , INF);
dst[s] = ;
for(int i = ; i <= N; i++){
int u = -, minLen = INF;
for(int j = ; j <= N; j++){
if(visit[j] == && dst[j] < minLen){
u = j;
minLen = dst[j];
}
}
if(u == -)
return;
visit[u] = ;
for(int j = ; j <= N; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].clear();
pre[j].push_back(u);
}else if(G[u][j] + dst[u] == dst[j]){
pre[j].push_back(u);
}
}
}
}
}
vector<int> path, ans;
int send = INF, back = INF;
void dfs(int vt){
path.push_back(vt);
if(vt == ){
int tempBack = , tempSend = , T = Cmax / ;
for(int i = path.size() - ; i >= ; i--){
if(v[path[i]] < T){
int shortage = T - v[path[i]];
if(shortage <= tempBack)
tempBack = tempBack - shortage;
else{
tempSend = tempSend + shortage - tempBack;
tempBack = ;
}
}else{
tempBack += v[path[i]] - T;
}
}
if(tempSend < send || tempSend == send && tempBack < back){
back = tempBack;
send = tempSend;
ans = path;
}
path.pop_back();
return;
}
for(int i = ; i < pre[vt].size(); i++){
dfs(pre[vt][i]);
}
path.pop_back();
}
int main(){
scanf("%d%d%d%d", &Cmax, &N, &Sp, &M);
for(int i = ; i <= N; i++){
scanf("%d", &v[i]);
}
fill(G[], G[] + *, INF);
for(int i = ; i < M; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
G[a][b] = G[b][a] = c;
}
dijkstra();
dfs(Sp);
printf("%d ", send);
for(int i = ans.size() - ; i > ; i--){
printf("%d->", ans[i]);
}
printf("%d %d", ans[], back);
cin >> N;
return ;
}

总结:

1、题意:求最短路,如果有多条,就求出需要发送自行车最少的一条,如果还有多条,就求出需要带回自行车最少的一条。 注意,路上某个节点多出来的自行车可以被它之后的节点补充。比如0点->A->B->C,如果B超了,C少了,可以把B多出来的给C补充,如果不够再从源点处拿。但如果A也少了,则不能把B多的补充给A,因为是按照源点->目的地的顺序一路前进。

2、错误:虽然节点是1到N, 0为管理处。但求最短路的时候,节点范围应从0到N。

另外,求自行车的send、back时,节点范围应从1到N。

另外,求自行车时,遍历的是 v[path[ i ]]而不是 v[ i ]。

3、读入图之前先对G做INF的初始化。

A1018. Public Bike Management的更多相关文章

  1. PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS

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

  2. PAT甲级——A1018 Public Bike Management

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

  3. [PAT] A1018 Public Bike Management

    [思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...

  4. PAT_A1018#Public Bike Management

    Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...

  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. 1018 Public Bike Management

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

  8. PAT 1018 Public Bike Management[难]

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

  9. PTA (Advanced Level) 1018 Public Bike Management

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

随机推荐

  1. 50分钟学会Laravel 50个小技巧(基于laravel5.2,仅供参考)

    转载请注明:转载自 Yuansir-web菜鸟 | LAMP学习笔记 本文链接地址: 50分钟学会Laravel 50个小技巧 原文链接:< 50 Laravel Tricks in 50 Mi ...

  2. Flutter上拉加载下拉刷新---flutter_easyrefresh

    前言 Flutter默认不支持上拉加载,下拉刷新也仅仅支持Material的一种样式.Android开发使用过SmartRefreshLayout的小伙伴都知道这是一个强大的刷新UI库,集成了很多出色 ...

  3. restful 规范(建议)

    需求:开发cmdb,对用户进行管理. 做前后端分离,后端写api(URL),对用户表进行增删改查,应该写四个URL(还要给文档(返回值,返回,请求成功,干嘛,失败,干嘛)),然后分别写视图函数. ht ...

  4. vue前端框架面试问题汇总

    1.active-class是哪个组件的属性?嵌套路由怎么定义?答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数? 答: ...

  5. servlet中将值以json格式传入

    详细连接https://blog.csdn.net/e_wsq/article/details/71038270$('#but_json_json').click(function(){ }; $.a ...

  6. java中间缓存变量机制

    public static void main(String[] args){ int j = 0; for(int i = 0; i < 100; i++) j = j++; System.o ...

  7. Python——Flask框架——程序的基本结构

    一.安装 pip install flask 二.初始化 from flask import Flask app = Flash(__name__) 三.路由:处理URL和函数之间的关系的程序称为路由 ...

  8. ES 6 系列 - Proxy

    Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改,所以是一种“元编程”,即对编程语言进行编程. 简单地理解,就是在目标对象之前假设一层“拦截”,外界对改对象的访问,都必须先通过这层拦截 ...

  9. Nintex History in Form Table

    一.设置参数 二.调用WebService 三.For Each 调用 四.拼写HTML Table 结果: 特别提示:过滤人只要根据人来循环即可

  10. spring事务 将多个connection放到一个线程中

    spring事务 将多个connection放到一个线程中