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 perfectcondition 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 S3, we have 2 different shortest paths:
-
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.
-
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 Tijwhich 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
Analyze:
First of all, we are supposed to find the shortest path by using Dijkstra Algorithm. The next step is to find the best route. We need to traceback to get the correct answer, so we can use depth-first-search. To calculate the number of bikes the PBMC needs to send or collect, we calculate the numnbers at each station and use recursion to get the final answer. The best way to get it is to understand what the problem requires. (If you can't pass case 7, you probably don't use dfs.)
#include<iostream>
#include<vector>
#define MAX_DIS 0X0FFFFFF
using namespace std;
int C, N, S, M, cur;
int min_need = MAX_DIS, min_surplus = MAX_DIS;
int capacity[501], station[501][501];
int visited[501], dis[501];
vector<int> path, temp_path;
vector<int> pre[501];
void Dfs(int index){
temp_path.push_back(index);
if(index == 0){
int need = 0, surplus = 0;
for(int i = temp_path.size() - 2; i >= 0; i--){
int k = temp_path[i];
if(capacity[k] > C / 2){
surplus += capacity[k] - C / 2;
}else{
if(surplus > (C / 2 - capacity[k])){
surplus -= (C / 2 - capacity[k]);
}else{
need += C / 2 - capacity[k] - surplus;
surplus = 0;
}
}
}
if((need < min_need) || (need == min_need && surplus < min_surplus)){
min_need = need;
min_surplus = surplus;
path = temp_path;
}
temp_path.pop_back();
return ;
}
for(int i = 0; i < pre[index].size(); i++)
Dfs(pre[index][i]);
temp_path.pop_back();
return ;
}
int main(){
cin >> C >> N >> S >> M;
//-----------Initialize-----------
for(int i = 1; i <= N; i++)
cin >> capacity[i];
for(int i = 1; i <= N; i++)
dis[i] = MAX_DIS;
for(int i = 0; i <= N; i++)
for(int j = 0; j <= N; j++)
station[i][j] = MAX_DIS;
for(int i = 1; i <= M; i++){
int s1, s2, t;
cin >> s1 >> s2 >> t;
station[s1][s2] = station[s2][s1] = t;
}
//------Find the shortest way-----
visited[0] = 1;
dis[0] = 0;
cur = 0;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(!visited[j] && station[cur][j] < MAX_DIS){
if(dis[j] > dis[cur] + station[cur][j]){
dis[j] = dis[cur] + station[cur][j];
pre[j].clear();
pre[j].push_back(cur);
}else if(dis[j] == dis[cur] + station[cur][j]){
pre[j].push_back(cur);
}
}
}
int index = -1, min_dis = MAX_DIS;
for(int j = 0; j <= N; j++){
if(!visited[j] && dis[j] < min_dis){
index = j;
min_dis = dis[j];
}
}
visited[index] = 1;
cur = index;
}
//--------------DFS----------------
Dfs(S);
//------------Output---------------
printf("%d 0", min_need);
for(int i = path.size() - 2; i >= 0; i--)
printf("->%d", path[i]);
printf(" %d", min_surplus);
return 0;
}
本文介绍了一种用于解决杭州城市公共自行车系统中车站容量失衡问题的算法。该算法通过寻找从公共自行车管理中心到问题车站的最短路径,并在路径上调整各车站的自行车数量,确保所有车站达到半满的理想状态。文章详细描述了算法的工作流程,包括使用Dijkstra算法寻找最短路径,以及通过深度优先搜索确定最优自行车调配方案。
840

被折叠的 条评论
为什么被折叠?



