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. linux audit审计(8)--开启audit对系统性能的影响

    我们使用测试性能的工具,unixbench,它有一下几项测试项目: Execl Throughput 每秒钟执行 execl 系统调用的次数 Pipe Throughput 一秒钟内一个进程向一个管道 ...

  2. macOS & USB stick

    macOS & USB stick why macOS can only read USB stick, can not write files to USB stick macos 无法写文 ...

  3. Java之XML操作:从XML中直接获取数据

    本文介绍如何将数据记录在XML文件中,然后通过DOM4J直接从XML中读取到数据. 依赖包: <dependency> <groupId>dom4j</groupId&g ...

  4. SECCON 2014 CTF:Shuffle

    很简单的一道小题 dia看一下是ELF文件 运行之: St0CFC}4cNOeE1WOS !eoCE{ CC T2hNto 是一串乱七八糟的字符 ida看一下: 很简单的逻辑 v5和v6是随机生成的两 ...

  5. Hibernate最佳实战

    1:一对一,一对多,多对多双向管理必设mappedBy ,将关系交给乙方维护,不然的话会在双方都建立关系,比如一对一双向的时候双方都会保存对方的id外键管理 具体在项目中采用双向还是单项看实际情况. ...

  6. ADO.NET工具类(三)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  7. Fourier Transform Complex Conjugate Discussion

    FT of function $f(t)$ is to take integration of the product of $f(t)$ and $e^{-j\Omega t}$. By separ ...

  8. Nginx grpc反向代理

    L111 首先Grpc 默认编译进Nginx 但是依赖http_v2模块 需要编译进nginx 具体指令可以参考Nginx http 反向代理 指令都类似 分布式反向代理 server { serve ...

  9. 双系统windows+linux如何正确删除linux

    双系统windows+linux如何正确删除linux 2017年11月16日 10:42:49 dovepym 阅读数:26363   之前在windows的基础上又安装了ubuntu系统16.04 ...

  10. EasyUI-datebox设置开始日期小于结束日期,并且结束日期小于当前日期

    datebox设置开始日期小于结束日期,并且结束日期小于当前日期 //日期控制扩展选择日期小于等于当前日期,开始日期小于等于结束日期 $("#datebox1").datebox( ...