PAT 1018. Public Bike Management (30)
1018. Public Bike Management (30)
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.
![](http://www.patest.cn/upload/11_lrrk0m5o4kg.jpg)
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 1Sample Output:
3 0->2->3 0
同样为最短路径问题,先采用dijkstra算法,计算出最短路径,其中不同的是这里会将相同的最短路径全部记录,即将pre设置为一个vector,记录所有的最短路。然后我们将所有的最短路径都取出来,计算每一个最短路径的sent from 和sent back,最后得到正确答案。
此题需要注意的是这里如果前面的车站车辆不够而在其后面的车站却又有多余的车辆,我们不能见后面的车辆拿到前面来,而应该从前面的车站和PBMS中拿,因而会出现from和back都不为0的情况。
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 const int INF = 0x7fffffff; 7 struct Station 8 { 9 int bikeNum; 10 int minTime = INF; 11 vector<int> pre; 12 bool visited = false; 13 }; 14 struct Path 15 { 16 vector<int> path; 17 int from; 18 int back; 19 Path(vector<int> path){ this->path = path; Update(); } 20 void Update(); 21 }; 22 23 int stationMap[501][501]; 24 Station stations[501]; 25 vector<Path> paths; 26 int half; 27 28 void FindPath(int destination, vector<int> path) 29 { 30 path.push_back(destination); 31 if (destination == 0) 32 paths.push_back(Path(vector<int>(path.rbegin(), path.rend()))); 33 for (int i = 0; i < stations[destination].pre.size(); i++) 34 FindPath(stations[destination].pre[i], path); 35 } 36 37 void Path::Update() 38 { 39 from = 0; 40 back = 0; 41 for (int i = 1; i < path.size(); i++) 42 { 43 int s = path[i]; 44 if (stations[s].bikeNum >= half) 45 back += (stations[s].bikeNum - half); 46 else if (back >= half - stations[s].bikeNum) 47 back -= (half - stations[s].bikeNum); 48 else 49 { 50 from += (half - stations[s].bikeNum - back); 51 back = 0; 52 } 53 } 54 } 55 56 int main() 57 { 58 int max, stationNum, destination, roadsNum; 59 cin >> max >> stationNum >> destination >> roadsNum; 60 half = max / 2; 61 for (int i = 1; i <= stationNum; i++) 62 cin >> stations[i].bikeNum; 63 //initialize the station map 64 for (int i = 0; i < 501; i++) 65 for (int j = 0; j < 501; j++) 66 stationMap[i][j] = stationMap[j][i] = INF; 67 for (int i = 0; i < roadsNum; i++) 68 { 69 int s1, s2, time; 70 cin >> s1 >> s2 >> time; 71 stationMap[s1][s2] = stationMap[s2][s1] = time; 72 } 73 74 //using dijkstra algorithm to get the minimum time path 75 //initialize 76 int min = 0; 77 stations[0].minTime = 0; 78 stations[0].visited = true; 79 while (1) 80 { 81 for (int i = 1; i <= stationNum; i++) 82 { 83 //update the time connnected to the station with minimum station 84 if (stationMap[min][i] < INF) 85 { 86 if (stations[min].minTime + stationMap[min][i] < stations[i].minTime) 87 { 88 stations[i].minTime = stations[min].minTime + stationMap[min][i]; 89 stations[i].pre.clear(); 90 stations[i].pre.push_back(min); 91 } 92 else if (stations[min].minTime + stationMap[min][i] == stations[i].minTime) 93 stations[i].pre.push_back(min); 94 } 95 } 96 //find the unknown station with minimum time 97 int minTime = INF; 98 for (int i = 1; i <= stationNum; i++) 99 { 100 if (!stations[i].visited && stations[i].minTime < minTime) 101 { 102 minTime = stations[i].minTime; 103 min = i; 104 } 105 } 106 stations[min].visited = true; 107 //all station are known 108 if (minTime == INF) 109 break; 110 } 111 112 FindPath(destination, vector<int>()); 113 int minFrom = INF, minBack = INF, minPath; 114 for (int i = 0; i < paths.size(); i++) 115 { 116 if (paths[i].from < minFrom) 117 { 118 minPath = i; 119 minFrom = paths[i].from; 120 minBack = paths[i].back; 121 } 122 else if (paths[i].from == minFrom && paths[i].back < minBack) 123 { 124 minPath = i; 125 minBack = paths[i].back; 126 } 127 } 128 129 cout << paths[minPath].from << " "; 130 for (int i = 0; i < paths[minPath].path.size()-1; i++) 131 cout << paths[minPath].path[i] << "->"; 132 cout << paths[minPath].path.back() << " "; 133 cout << paths[minPath].back; 134 }