https://www.nowcoder.com/pat/5/problem/4324
题目大意:
共享单车管理中心对于每一个车站那,进行管理,完美的状态是只有一半车,如果一个车站是满的或者空的,管理中心就会把那个车站调解成为完美车站,而且所有路过的车站都会被调整.管理中心会选择最短的路径去到达那个站点,不止一条最短路径时,会选择需要最少数量的车的那条路径,
题目描述
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.
上图为例一,车站为点路为边,为了解决站点3的问题,我们有两种方案,最终选择了从s2->s3因为需要的车辆少.
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:
- 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.
输入描述:
第一行有四个值:
1.每一个的站点的最大值Cmax(永远是偶数)
2.站点的个数N(不包括PBMC)
3.问题站点的坐标Sp
4.道路的数量M
第二行包含N个非负数,分别代表每一个站点的数量
然后接下来的M行,每一个包括3个数
Si,Sj,Tij,Tij代表从Si到Sj的时间
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.
首先输出PBMC必须发出的车数量,然后输出路径,然后输出要带回PBMC的数量
如果路径不是唯一的,输出最少的数量需要带回去的.
输出描述:
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.
输入例子:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
输出例子:
3 0->2->3 0
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static int cmax, n, sp, m;
private static int costtimes, outbikes, inbikes;
private static int resulttimes = Integer.MAX_VALUE;
private static int resultoutbikes, resultinbikes;
private static int[][] times;
private static int[] stations;
private static boolean[] visited;
private static List<Integer> path = new ArrayList<>();
private static List<Integer> resultpath;
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] s = buf.readLine().split(" ");
cmax = Integer.parseInt(s[0]);
n = Integer.parseInt(s[1]);
sp = Integer.parseInt(s[2]);
m = Integer.parseInt(s[3]);
stations = new int[n + 1];
times = new int[n + 1][n + 1];
visited = new boolean[n + 1];
s = buf.readLine().split(" ");
for (int i = 1; i <= n; i++) {
stations[i] = Integer.parseInt(s[i - 1]);
}
for (int i = 1; i <= m; i++) {
s = buf.readLine().split(" ");
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
int d = Integer.parseInt(s[2]);
times[a][b] = d;
times[b][a] = d;
}
dfs(0, 0, sp);
System.out.print(resultoutbikes + " " + "0");
for (int i = 1; i < resultpath.size(); i++) {
System.out.print("->" + resultpath.get(i));
}
System.out.println(" " + resultinbikes);
}
private static void dfs(int start, int index, int end) {
visited[index] = true;
path.add(index);
costtimes += times[start][index];
if (index == end) {
inbikes = 0;
outbikes = 0;
for (int i = 1; i < path.size(); i++) {
if (stations[path.get(i)] > cmax / 2) {
inbikes += stations[path.get(i)] - cmax / 2;
} else if (cmax / 2 - stations[path.get(i)] - inbikes <= 0) {
inbikes -= cmax / 2 - stations[path.get(i)];
} else {
outbikes += cmax / 2 - stations[path.get(i)] - inbikes;
inbikes = 0;
}
}
if (costtimes == resulttimes) {
//首先保证发出来的数量要最少,如果发出来的数量大,即使inbikes小,也不管它
if (outbikes != resultoutbikes) {
if (outbikes<resultoutbikes) {
resultpath = new ArrayList<>(path);
resultoutbikes = outbikes;
resultinbikes = inbikes;
}
} else if (inbikes < resultinbikes) {
resultpath = new ArrayList<>(path);
resultinbikes = inbikes;
}
} else if (costtimes < resulttimes) {
resultpath = new ArrayList<>(path);
resulttimes = costtimes;
resultinbikes = inbikes;
resultoutbikes = outbikes;
}
} else {
for (int i = 1; i <= n; i++) {
if (times[index][i] != 0 && !visited[i]) {
dfs(index, i, end);
}
}
}
path.remove(path.size() - 1);
visited[index] = false;
costtimes -= times[start][index];
}
}