PAT 甲级 1018. Public Bike Management (30) C++版

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

用Dijkstra查找最短路径,在更新路径的同时更新结点的sent和take back的状态。如果存在多条路径的长度相等,选择需要从0结点携带的自行车的数目最少的路径。如果存在多条路径的长度相等且需要携带的自行车数目也相等,那么需要选择从问题结点带回的自行车数目最少的路径。 case 7 没有过。问题在于Dijkstra算法的执行过程中,更新的sent和take back的状态不具有最有子结构,即局部最优无法得到全局最优。 对于测试用例: 10 4 4 5 4 8 9 0 0 1 1 1 2 1 1 3 2 2 3 1 3 4 1 在3号结点更新的时候,应该选择2号结点,而不是1号结点,虽然从1号结点过来可以得到最少的take back的自行车的数目,但是不是全局最优的。代码如下:

正确的代码,使用DFS的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <vector>

using namespace std;

const int inf = 99999999;
int graph[510][510];
int weight[510];
bool visited[510];

void initGraph() {
for (int i = 0; i < 510; i++)
for (int j = 0; j < 510; j++)
graph[i][j] = inf;
}

vector<int> path, tPath;
int sent = inf, back = inf, shortest = inf;

void dfs(int cur, int sp, int n, int tlen, int tSent, int tBack) {
if (cur == sp) {
if (shortest > tlen) {
shortest = tlen;
path = tPath;
sent = tSent;
back = tBack;
} else if (shortest == tlen) {
if (sent > tSent) {
sent = tSent;
back = tBack;
path = tPath;
} else if (sent == tSent) {
if (back > tBack) {
back = tBack;
path = tPath;
}
}
}
} else {
for (int i = 1; i <= n; i++) {
if (visited[i] == false && graph[cur][i] != inf) {
visited[i] = true;
tPath.push_back(i);
if (weight[i] <= 0) {
if (abs(weight[i]) <= tBack) {
dfs(i, sp, n, tlen + graph[cur][i], tSent, tBack - abs(weight[i]));
} else {
dfs(i, sp, n, tlen + graph[cur][i], tSent + abs(weight[i]) - tBack, 0);
}
} else {
dfs(i, sp, n, tlen + graph[cur][i], tSent, tBack + abs(weight[i]));
}
visited[i] = false;
tPath.pop_back();
}
}
}
}

int main() {
int cmax = 0, n = 0, sp = 0, m = 0, si = 0, sj = 0, tij = 0;;
scanf("%d %d %d %d", &cmax, &n, &sp, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &weight[i]);
weight[i] -= cmax / 2;
}
initGraph();
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &si, &sj, &tij);
graph[si][sj] = graph[sj][si] = tij;
}
dfs(0, sp, n, 0, 0, 0);
printf("%d ", sent);
printf("0");
for (int i = 0; i < path.size(); i++) {
printf("->%d", path[i]);
}
printf(" %d\n", back);
return 0;
}