程序设计基础30 tips 图的最短路径(二)

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

一,主要思想

    这道题的最短路径套模板即可,但是关于第二指标却不好筛选,题目中是让路径长度相同时需要自行车数目越少越好,若是都不需要自行车,则送回总站的自行车数目越少越好。

    首先我考虑的是算出各个站点自行车数目总量和应该布局自行车的总量进行加减。但是出现的问题是需要甄别是负数的情况,若是负数时需要比较最大值,若是整数时需要比较最小值,还需要加上flag判定,太麻烦。

void DFS(int v) {
	if (v == 0) {
		int sum_num = tempPath.size();
		int sum_half = sum_num*c_max / 2;
		int sum_total = 0;
		for (int i = tempPath.size() - 1; i >= 0; i--) {
			int id = tempPath[i];
			sum_total += current_num[id];
		}
		int middle_1 = sum_half - sum_total;
		if (flag==1) {
			if (middle_1 < 0) {
				flag = 0;
				middle = middle_1;
				path = tempPath;
			}
			else if (middle_1 >= 0 && middle > middle_1) {
				middle = middle_1;
				path = tempPath;
			}
		}
		else if (flag == 0) {
			if (middle_1<0 && middle_1>enmiddle) {
				enmiddle = middle_1;
				path = tempPath;
			}
		}
		return;
	}
	tempPath.push_back(v);
	for (set<int>::iterator it = pre[v].begin(); it != pre[v].end(); it++) {
		DFS(*it);
	}
	tempPath.pop_back();
}

    为了不再flag判定,我选择了在遍历中算出总共需要多少,总共返回多少,然后进行比较加减不会出现负数。

void DFS(int v) {
	if (v == 0) {
		int send = 0, take = 0;
		for (int i = tempPath.size() - 1; i >= 0; i--) {
			int id = tempPath[i];
			if (current_num[id] > c_max/2) {
				take += (current_num[id] - c_max / 2);
			}
			else if (current_num[id] < c_max / 2) {
				send += (c_max / 2 - current_num[id]);
			}
		}
		if (send >= take) {
			send -= take;
			take = 0;
		}
		else {
			take -= send;
			send = 0;
		}
		if (send < min_send) {
			min_send = send;
			min_take = take;
			path = tempPath;
		}
		else if (send == min_send&&take < min_take) {
			min_take = take;
			path = tempPath;
		}
		return;
	}
	tempPath.push_back(v);
	for (set<int>::iterator it = pre[v].begin(); it != pre[v].end(); it++) {
		DFS(*it);
	}
	tempPath.pop_back();
}

但是这种方法的前提是考虑往返,所以一个站点若是不够,可以在返的时候用其他的站点多出来的补上。但题目是只有往没有返,所有不能从全局考虑 。

void DFS(int v) {
	if (v == 0) {
		int len = tempPath.size(), tsum = 0, trequire = 0;
		for (int i = len - 1; i >= 0; i--) {
			if (current_num[tempPath[i]] >= c_max / 2) {
				tsum += current_num[tempPath[i]] - c_max / 2;
			}
			else {
				if (tsum < c_max / 2 - current_num[tempPath[i]]) {
					trequire += (c_max / 2 - current_num[tempPath[i]] - tsum);
					tsum = 0;
				}
				else {
					tsum -= (c_max / 2 - current_num[tempPath[i]]);
				}
			}
		}
		if (trequire < frequire) {
			frequire = trequire;
			path = tempPath;
			freturn = tsum;
		}
		else if (trequire == frequire&&tsum < freturn) {
			path = tempPath;
			freturn = tsum;
		}
		return;
	}
	tempPath.push_back(v);
	for (set<int>::iterator it = pre[v].begin(); it != pre[v].end(); it++) {
		DFS(*it);
	}
	tempPath.pop_back();
}

二,正确代码 :

#include<cstdio>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int max_n = 510;
const int INF = 1000000000;
int	c_max = 0;
int N = 0;
int frequire = INF;
int freturn = INF;
int current_num[max_n];
int times[max_n];
bool vis[max_n] = { false };
int G[max_n][max_n] = { 0 };
vector<int> path, tempPath;
set<int> pre[max_n];
queue<int> que;
int SPFA(int s) {
	fill(times, times + max_n, INF);
	que.push(s);
	times[s] = 0;
	vis[s] = true;
	times[s] = 0;
	while (!que.empty()) {
		int u = que.front();
		que.pop();
		vis[u] = false;
		for (int j = 0; j <= N; j++) {
			if (G[u][j] != 0) {
				int t = G[u][j];
				if (times[u] + t < times[j]) {
					times[j] = times[u] + t;
					pre[j].clear();
					pre[j].insert(u);
					if (!vis[j]) {
						que.push(j);
						vis[j] = true;
					}
				}
				else if (times[u] + t == times[j]) {
					pre[j].insert(u);
				}
			}
		}
	}
}
void DFS(int v) {
	if (v == 0) {
		int len = tempPath.size(), tsum = 0, trequire = 0;
		for (int i = len - 1; i >= 0; i--) {
			if (current_num[tempPath[i]] >= c_max / 2) {
				tsum += current_num[tempPath[i]] - c_max / 2;
			}
			else {
				if (tsum < c_max / 2 - current_num[tempPath[i]]) {
					trequire += (c_max / 2 - current_num[tempPath[i]] - tsum);
					tsum = 0;
				}
				else {
					tsum -= (c_max / 2 - current_num[tempPath[i]]);
				}
			}
		}
		if (trequire < frequire) {
			frequire = trequire;
			path = tempPath;
			freturn = tsum;
		}
		else if (trequire == frequire&&tsum < freturn) {
			path = tempPath;
			freturn = tsum;
		}
		return;
	}
	tempPath.push_back(v);
	for (set<int>::iterator it = pre[v].begin(); it != pre[v].end(); it++) {
		DFS(*it);
	}
	tempPath.pop_back();
}
int main() {
	int broken = 0, M = 0;
	int x = 0, y = 0, z = 0;
	scanf("%d %d %d %d", &c_max, &N, &broken, &M);
	for (int i = 1; i <= N; i++) {
		scanf("%d", &current_num[i]);
	}
	for (int i = 0; i < M; i++) {
		scanf("%d %d %d", &x, &y, &z);
		G[x][y] = z;
		G[y][x] = z;
	}
	SPFA(0);
	DFS(broken);
	printf("%d ", frequire);
	printf("0->");
	for (int i = path.size() - 1; i >= 0; i--) {
		printf("%d", path[i]);
		if (i != 0) {
			printf("->");
		}
		else {
			printf(" ");
		}
	}
	printf("%d", freturn);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq2285580599/article/details/82938974