PAT A1018

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

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "stdio.h"
#include "math.h"
#include "string.h"
#include "iostream"
//#include "stdlib.h"
#include "vector"
#include "set"
#include "map"
//#include "stack"
#include "queue"
#include "algorithm"
using namespace std;

const int MAXV = 510;//最大顶点数
const int INF = 1000000000;//无穷大

//n为顶点数,m为边数,Cmax为最大容量,Sp为问题站点
//G为邻接矩阵,weight为点权,d[]记录最短距离
//mindNeed记录最少携带数目,minRemain记录最少带回的数目
int n, m , Cmax, Sp, numPath = 0, G[MAXV][MAXV], weight[MAXV];
int d[MAXV], minNeed = INF, minRemain = INF;
bool vis[MAXV] = {false};//vis[i] == true 表示顶点i已访问,初始均为false
vector<int> pre[MAXV];//前驱
vector<int> tempPath, path;//临时路径及最优路径

void Dijkstra(int s){//s为起点
fill(d, d + MAXV, INF);
d[s] = 0;
for (int i = 0; i < n; i++) {//循环n次
int u = -1, MIN = INF;//u使d[u]最小,MIN存放该最小的d[u]
for (int j = 0; j <= n; j++) {//找到未访问的顶点中d[]最小的
if (vis[j] == false && d[j] < MIN) {
u = j;
MIN = d[j];
}

}
//找不到小于INF的d[u],说明剩下的顶点和起点s不连通
if (u == -1) return;
vis[u] = true;//标记u已访问
for (int v = 0; v <= n; v++) {
//如果v未访问&& u能到达v
if (vis[v] == false && G[u][v] != INF) {
if (d[u] + G[u][v] < d[v]) {
d[v] = d[u] + G[u][v];//优化d[v]
pre[v].clear();
pre[v].push_back(u);
}else if (d[u] + G[u][v] == d[v]){
pre[v].push_back(u);
}
}
}
}
}
void DFS(int v){
if(v == 0){//递归边界,叶子结点
tempPath.push_back(v);
//路径tempPath上需要携带数目、需要带回的数目
int need = 0, remain = 0;
for (int i = tempPath.size() - 1; i >= 0 ; i--) {
int id = tempPath[i];//当前结点编号id
if (weight[id] > 0) {//点权大于0,说明需要带走一部分自行车
remain += weight[id];
}else{//点权不超过0,需要补给
if (remain > abs(weight[id])) {//当前持有量足够补给
remain -= abs(weight[id]);
}else{
need += abs(weight[id]) - remain;//不够的部分从PBMC携带
remain = 0;//当前持有的自行车都用来补给
}
}
}
if (need < minNeed){//需要从PBMC携带的自行车数目更少
minNeed = need;//优化minNeed
minRemain = remain;//覆盖minRemain
path = tempPath;//覆盖最优路径path
}else if(need == minNeed && remain < minRemain){
//携带数目相同,带回数目变少
minRemain = remain;//优化minRemain
path = tempPath;//覆盖最优路径path
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for (int i = 0; i < pre[v].size(); i++) {
DFS(pre[v][i]);
}
tempPath.pop_back();
}

int main(){
scanf("%d%d%d%d", &Cmax, &n, &Sp, &m);
int u, v;
fill(G[0], G[0] + MAXV * MAXV, INF);
for (int i = 1; i <= n; i++) {
scanf("%d", &weight[i]);
weight[i] -= Cmax / 2;//点权减去答案的一半
}
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
scanf("%d", &G[u][v]);
G[v][u] = G[u][v];
}
Dijkstra(0);//Dijkstra算法入口
DFS(Sp);
printf("%d ", minNeed);
for (int i = path.size() - 1; i >= 0; i--) {
printf("%d", path[i]);
if (i > 0) {
printf("->");
}
}
printf(" %d\n", minRemain);
return 0;
}