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.
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 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 Spis 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
一开始想利用贪心算法让车数量与时间同时最优,测试时发现在某些情况下车数量并不能最优,
如:10 5 4 5
6 7 5 0
0 1 1
0 2 1
1 3 1
2 3 1
3 4 1
于是考虑先记录所有最短路径,然后再找出车数量最优的路径
代码如下:
#include
#include
#define INIFINITY 10000000
struct {
int parent;//record parent
int Col;//record different ways
int send;//record num of sending bikes
int back;//record num of back bikes
} path[1000][1000];//record all the shortest path
int C, N, sp, M, G[1000][1000], cur[1000];
int time[1000], known[1000], count[1000];
void findpath(int x);//find the shortest ways
int main(void){
int v, w, i, j, k, l;
int way[1000];//record the aim way
int send, back, pre, prep;
scanf("%d%d%d%d", &C, &N, &sp, &M);//get the Capacity of each station,the number of stations,the problem station,the number of edge
for (i = 1; i <= N; i++) {
scanf("%d", &cur[i]);//get the number of bikes of each station
time[i] = -1;
known[i] = 0;
count[i] = 0;//initialnize
}
//initialnize
for (i = 1; i <= N; i++){
for (j = 1; j <= N; j++) {
G[i][j] = 0;
path[i][j].parent = path[i][j].Col = 0;
path[i][j].send = path[i][j].back = 0;
}
}
for (i = 0; i < M; i++) {
scanf("%d %d %d", &v, &w, &j);
G[w][v] = G[v][w] = j;
}//record the edges
findpath(sp);//find the shortest path
//find the best way
send = back = INIFINITY;
for (i = 0; i < count[sp]; i++) {
if (send > path[sp][i].send ||(send == path[sp][i].send && back > path[sp][i].back)) {
send = path[sp][i].send;
back = path[sp][i].back;
pre = path[sp][i].parent;
prep = path[sp][i].Col;
}
}
for (i = pre, j = prep, k = 0; i >= 0; ) {
way[k++] = i;
pre = path[i][j].parent;
prep = path[i][j].Col;
i = pre;
j = prep;
}
//print the ways and the send bikes number and the back bikes number
printf("%d ", send);
for (--k; k >= 0; k--)
printf("%d->", way[k]);
printf("%d %d\n", sp, back);
return 0;
}
void findpath(int x)
{
int v, w, i, j, k, len, temp;
//initialnize the start
time[0] = 0;
path[0][0].parent = -1;
path[0][0].Col = -1;
path[0][0].send = path[0][0].back = 0;
count[0] = 1;
//find the index of minimum time
while (!known[x]) {
len = INIFINITY;
for (i = 0; i <= N; i++) {
if (known[i])
continue;
if (time[i] >= 0 && time[i] < len) {
len = time[i];
v = i;
}
}
//mark it as known
known[v] = 1;
//find time waving ways
for (w = 1; w <= N; w++) {
if (!G[v][w] || known[w])//if two station are not connected or the station already know ,do nothing
continue;
if (time[w] >= 0 && time[w] < time[v] + G[v][w])//if time>0 and time is shorter ,do nothing
continue;
else if (time[w] == -1 || time[w] >= time[v] + G[v][w]) {
if (time[w] == -1 || time[w] > time[v] + G[v][w]) //if time==-1(means this is the first way),or time is longer ,setcount[w]==0
count[w] = 0;
time[w] = time[v] + G[v][w];//update the time
temp = fabs(cur[w] - C/2);
for (i = 0, j = count[w]; i < count[v]; i++, j++) {
path[w][j].parent = v;
path[w][j].Col = i;//record the path (parent if path[parent][Col])
//caculate the needing changes of bike bumber
if (cur[w] >= C/2) {
path[w][j].back = path[v][i].back + temp;
path[w][j].send = path[v][i].send;
}
else {
if (temp <= path[v][i].back) {
path[w][j].back = path[v][i].back - temp;
path[w][j].send = path[v][i].send;
}
else {
path[w][j].send = path[v][i].send + temp - path[v][i].back;
path[w][j].back = 0;
}
}
}
count[w] = j;//record the kinds of ways
}
}
}
}