A1018. Public Bike Management
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的更多相关文章
- PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT甲级——A1018 Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- [PAT] A1018 Public Bike Management
[思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...
- PAT_A1018#Public Bike Management
Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...
- 1018. Public Bike Management (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...
- PAT 1018. Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- 1018 Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PTA (Advanced Level) 1018 Public Bike Management
Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...
随机推荐
- jenkins配置SSH远程服务器连接
之前用jenkins做了一个自动发布测试,配置任务的Post Steps时,选择的是执行shell命令.如下图: 这是在本192.168.26.233服务器上测试的,此服务器上运行jenkins,to ...
- sqlserver常用语法
--临时表 IF OBJECT_ID('tempdb..#Entry') is not null BEGIN DROP TABLE #Entry END ------------------- ...
- 剑指offer(19)二叉树中和为某一值的路径
题目: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大的 ...
- rem 自适应、整体缩放
html{ font-size: calc(100vw/7.5); } 说明: 100vw是设备的宽度,除以7.5可以让1rem的大小在iPhone6下等于100px. 若是低版本的设备不支持rem, ...
- 关于IWMS后台登录问题总结
一.登录后台,点击登录无反应: 1.是因为网站文件夹没有权限,需要右击文件夹,将只读勾选去掉 2.在安全中加入Everyone对象. 二.登录后台后,左边显示不全,是因为会员权限不够,需要给权限.
- python数学第四天【古典概型】
- Fiddler 学习笔记---命令、断点
输入命令框: 1 输入 ?51testing 高亮显示对应记录 2 >10 选择body大于10的记录 3 <10 选择body<10的记录 4 =200 选择result=200 ...
- scrapy 项目搭建
安装好scrapy后,开始创建项目 项目名:zhaopin 爬虫文件名:zhao 1:cmd -- scrapy startproject zhaopin 2:cd zhaopin,进入项目目 ...
- HTTP协议 - 基础认识
在http协议使用场景上我们最熟悉的可能就是浏览器了,作为本系列第一篇,就讲一个问题 ”浏览器怎么连接上服务器并获取网页内容的“ : 首先 浏览器怎么连接上服务器的? 如果对OSI七层模型或者TCP ...
- telerik reporting报表
Telerik Reporting是一个非常人性化的控件,一个报表的生成几乎不用写代码,都是通过"所见即所得"模式完成.由于客户需要在实际的项目中运用Telerik Reporti ...