1018 Public Bike Management (Dijkstra和DFS)

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.

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 S​3​​, we have 2 different shortest paths:

  1. PBMC -> S​1​​ -> S​3​​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S​1​​ and then take 5 bikes to S​3​​, so that both stations will be in perfect conditions.

  2. PBMC -> S​2​​ -> S​3​​. 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: C​max​​ (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; S​p​​, 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 C​i​​ (i=1,⋯,N) where each C​i​​ is the current number of bikes at S​i​​ respectively. Then M lines follow, each contains 3 numbers: S​i​​, S​j​​, and T​ij​​ which describe the time T​ij​​ taken to move betwen stations S​i​​ and S​j​​. 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−>S​1​​−>⋯−>S​p​​. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S​p​​ 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、 管理人员的路线是单向的,也就是只能从0节点到问题节点,只能把前面节点的自行车匀给后面节点。

3、用Dijstra把所有最短路径存起来,再按照优先级比较每条路径。由于minneed和minback的值跟每个节点都有关系,不能使用贪心算法,只用Dijkstra是不行的,需要存起所有的最短路径,再DFS比较。

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;


public class Main {

	static int C;
	static int N;
	static int S;
	static int M;
	static int num[];
	static int G[][];
	static boolean collect[];
	static ArrayList<Integer> parent[];
	static int dist[];
	static Bic bic[];
	static boolean visited[];

	
	static int minneed = Integer.MIN_VALUE, minback = Integer.MAX_VALUE;
	static ArrayList<Integer> fa = new ArrayList<>();

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		in.nextToken();
		C = (int) in.nval;
		in.nextToken();
		N = (int) in.nval;
		in.nextToken();
		S = (int) in.nval;
		in.nextToken();
		M = (int) in.nval;
		num = new int[N + 1];
		dist = new int[N + 1];

		collect = new boolean[N + 1];
		G = new int[N + 1][N + 1];
		bic = new Bic[N + 1];

		for (int i = 1; i < N + 1; i++) {
			in.nextToken();
			num[i] = (int)in.nval - C / 2;
			dist[i] = Integer.MAX_VALUE / 10;
		}
		dist[0] = 0;
		parent = new ArrayList [N + 1];
		for (int i = 0; i < N + 1; i++) {
			parent[i] = new ArrayList<Integer>();
		}
		parent[0].add(-1);

		for (int i = 0; i < N + 1; i++) {
			for (int j = 0; j < N + 1; j++) {
				G[i][j] = Integer.MAX_VALUE / 10;
				if (i == j) {
					G[i][j] = 0;
				}
			}
		}

		for (int i = 0; i < M; i++) {
			in.nextToken();
			int x = (int) in.nval;
			in.nextToken();
			int y = (int) in.nval;
			in.nextToken();
			int value = (int) in.nval;
			G[x][y] = value;
			G[y][x] = value;
		}

		for (int i = 0; i < N + 1; i++) {
			dist[i] = G[0][i];
			bic[i] = new Bic();

		}

		while (true) {
			int v = mindist();
			if (v == -1) {
				break;
			}
			collect[v] = true;

			for (int i = 0; i < N + 1; i++) {
				if (G[v][i] < Integer.MAX_VALUE / 10 && v != i && collect[i] == false) {
					if (dist[i] > dist[v] + G[v][i]) {
						dist[i] = dist[v] + G[v][i];
						parent[i].clear();
						parent[i].add(v);

					} else if (dist[i] == dist[v] + G[v][i]) {
						parent[i].add(v);
					}
				}
			}

		}
		ArrayList<Integer> al = new ArrayList<>();
		al.add(S);
		dfs(S, al);

		String str ="";
		int n = fa.size();
		for (int i = n - 1; i >= 0; i--) {
			if(i!=0)
			str = str + fa.get(i)+"->";
			else
				str+=fa.get(i);
		}

		System.out.println(-minneed + " " + str + " " + minback);

	}

	private static void dfs(int root, ArrayList<Integer> alist) {
		// TODO Auto-generated method stub
		if (root == 0) {

			int sum = 0, temp = 0, sub = 0;
			int n = alist.size();
			for (int i = n - 1; i >= 0; i--) {
				int index = alist.get(i);
				sum += num[index];
				if (sum < temp) {
					temp = sum;
				}

				sub += num[index];
				if (sub <= 0) {
					sub = 0;
				}
			}

			if (temp > minneed) {
				minneed = temp;
				fa.clear();
				fa.addAll(alist);
				minback = sub;
			}

			else if (temp == minneed) {
				if (sub < minback) {
					fa.clear();
					fa.addAll(alist);
					minback = sub;
				}
			}

		} else {

			int n = parent[root].size();

			for (int i = 1; i < n; i++) {
				ArrayList<Integer> al = new ArrayList<>();
				al.addAll(alist);
				al.add(parent[root].get(i));
				dfs(parent[root].get(i), al);
			}
			alist.add(parent[root].get(0));
			dfs(parent[root].get(0), alist);
		}
	}

	private static int mindist() {
		// TODO Auto-generated method stub
		int index = -1;
		int temp = Integer.MAX_VALUE / 10;
		for (int i = 0; i < N + 1; i++) {
			if (dist[i] < temp && collect[i] == false) {
				temp = dist[i];
				index = i;
			}
		}

		return index;
	}

}

class Bic {
	int big = 0;
	int small = 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38902950/article/details/87894086