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. webpack+vue 我的视角(持续更新)

    最近一直在研究webpack+vue的组合拳,现在分享一下: webpack就是一个项目管理工具,可以各种模块化加载,然后压缩,当然还有热加载技术(时灵时不灵..) vue是mv*模式的框架,组件化开 ...

  2. DAY08、文件操作

    一.文件操作模式汇总: 主模式: r:读模式 w:写模式(无创建,有清空) a:追加(有创建的功能) x:写,必须自己创建文件,否则报错 从模式: t:文本操作(默认模式)r >rt,w> ...

  3. python数据结构与算法第七天【链表】

    1.链表的定义 如图: 注意: (1)线性表包括顺序表和链表 (2)顺序表是将元素顺序地存放在一块连续的存储区里 (3)链表是将元素存放在通过链构造的存储快中 2. 单向链表的实现 #!/usr/bi ...

  4. Eclipse:报错Failed to read artifact descriptor for org.springframework.boot:spring-boot-autoconfigure:jar:2.1.2.

    导入SVN下载的MAVEN项目时springboot报错: pom.xml文件报错 Failed to read artifact descriptor for org.springframework ...

  5. LODOP打印控件进行批量打印

    Lodop打印控件批量打印的方式:1.批量打印每页内容相同的:(1)批量打印相同内容的很多纸张,可以设置打印份数,把该内容打印出多份.2.批量打印每页不同内容的:(1)通过在一个任务中分页,循环添加页 ...

  6. css居中小技巧

    一.行内元素-水平居中 在父元素的样式中添加: text-align:center; 二.定宽块级元素-水平居中 所谓定宽块级元素指块级元素的宽度指定,而不是默认的100%,否则此方法无效: 代码: ...

  7. 学习 Spring (十六) AOP API

    Spring入门篇 学习笔记 Spring AOP API 是 Spring 1.2 历史用法,现在仍然支持 这是 Spring AOP 基础,现在的用法也是基于历史的,只是更简便了 Pointcut ...

  8. Nginx websocket反向代理

    L:106 现在主流的反向代理,通过长链接可以从服务器推送数据到页面 升级成websocket反向代理必须根据上面的配置做配置 缺点无法多路复用,也就是没办法并行 我们测试下Websocket反向代理 ...

  9. 数据库中事务的四大特性(ACID)

    本篇讲诉数据库中事务的四大特性(ACID),并且将会详细地说明事务的隔离级别. 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ 原子性(Atomicity) 原子性是指事务 ...

  10. [BZOJ 2186] [SDOI 2008] 沙拉公主的困惑

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为 \(1\) 到 \(N\) 的阶乘,但是,政府只发行编号与 \(M!\) 互质的钞票.房地产第 ...