**甲级PAT 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

 题目要求:

每个自行车站最大的容量为Cmax(偶数),当自行车站的数量为Cmax/2时达到完美状态。给定一个有一个自行车站sp的数量为满,或者空,公共自行车管理中心就要去这个自行车车站通过带自行车过去或者带自行车回去使该车站达到完美状态。公共自行车管理中心位于0处,要求从0出发到达sp的路线最短,其中从0出发到达sp的过程中经过的车站均需为完美状态,到达sp后直接回到0处,如果有多条路线距离相等,则选择需要带出自行车最少的,如果还有相等的路线,则选择需要带回自行车最少的。

输出需要带出的自行车数,经过的车站顺序,需要带回的自行车数。

解题思路:

这里涉及到最短路径,需要用的Dijkstra算法。找出最短的路径,但是这里的难点是不仅需要找出最短路径的路线还需要找到最小带出自行车数和最小的带回自行车数。

错误思路:

最开始想的是通过动态规划,用一个dp状态,选的策略有以下几种:

1.目标车站状态是满的时候,此时需要当然优先选路线上都是完美的情况,或者路线上超出完美之上与完美差最少的,否则就选在完美下与完美差的最少的。

2.目标车站状态为空的时候,此时需要当然优先选路线上整体的和恰好能多出cmax/2的,或者路线上超出完美之上与完美差最少的,否则就选在完美下与完美差的最少的。

但是路径的局部最优并不意味着全局最优。可能出现像当目标车站为满,路线上有一个车站超出完美很多,但有另个一个车站低于完美很多,两个互补正好都是完美状态。

所有该方法不行。

正确思路:

用一个二维数组,存储每个车站在最短路径的时候可能的车站。这里用vector方便当找到更短路径时清空该车站之前的数组。这样就能记录最短路径的所有可能情况。然后用dfs遍历,从sp开始,找出到0时,记录当前路径,如果当前路径的need小于最小的,则将当前路径替换最小路径。若need相等,若back要小于最小的,则将当前路径替换最小路径。就这样将所有路径遍历,最后输出。

注意:

1.在dfs寻找最优解的时候,当找到0的时候,返回上一层要将0从数组中去除。temproute.pop_back();return;

2.在每个车站之前所有的情况已经遍历后也要从数组中去除。temproute.pop_back();

完整代码:

#include<iostream>
#include<vector> 
using namespace std;
#define maxn 501
#define INF 999999 

int road[maxn][maxn];
int bikenum[maxn];
int dst[maxn];
int book[maxn];
vector<int> front[maxn];
vector<int> route,temproute;
int minNeed = INF,minBack = INF;

void dfs(int x){
	int i,j,need,back,id;
	temproute.push_back(x);
	if(x == 0){
		need = 0;back = 0;
		for(j = temproute.size()-2;j>=0;j--){
			id = temproute[j];
			if(bikenum[id] >0){
				back+= bikenum[id];
			}else{
				if(back > (0-bikenum[id])){
					back += bikenum[id];
				}else{
					need += (0-bikenum[id]) - back;
					back = 0;
				}
			}
		}
		if(need < minNeed){
			minNeed = need;
			minBack = back;
			route = temproute;
		}else if(need == minNeed){
			if(back < minBack){
				minBack = back;
				route = temproute;
			}
		}
		temproute.pop_back();
		return;
	}
	for(i=0;i<front[x].size();i++){
		dfs(front[x][i]);
	}
	temproute.pop_back();
}

int main(){
	int cmax,N,M,sp;
	int i,j,k,v1,v2,min,u,temp,num,sum,standard;
	cin>>cmax>>N>>sp>>M;
	standard = cmax/2;
	for(i=0;i<=N;i++){
		book[i] = 0;
		for(j=0;j<=i;j++){
			road[i][j] = road[j][i] = INF;
		}
	}
	for(i=1;i<=N;i++){
		cin>>bikenum[i];
		bikenum[i] = bikenum[i] - cmax/2;
	}
	for(i=1;i<=M;i++){
		cin>>v1>>v2;
		cin>>road[v1][v2];
		road[v2][v1] = road[v1][v2];
	}
	for(i=1;i<=N;i++){
		dst[i] = road[0][i];
		if(road[0][i]!=INF){
			front[i].push_back(0);
		}
	}
	book[0] = 1;
	for(i=1;i<=N-1;i++){
		min = INF;
		for(j=1;j<=N;j++){
			if(book[j] == 0 && min > dst[j]){
				min = dst[j];
				u = j;
			}
		}
		book[u] = 1;
		for(k=1;k<=N;k++){
			if(road[u][k] < INF && book[k] == 0){
				if(dst[u] + road[u][k] < dst[k]){
					dst[k] = dst[u] + road[u][k];
					front[k].clear();
					front[k].push_back(u);
				}else if(dst[u] + road[u][k] == dst[k]){
					front[k].push_back(u);
				}
			}
		}
	}
	dfs(sp);	
	cout<<minNeed<<" ";
	for(i=route.size()-1;i>=1;i--){
		cout<<route[i]<<"->";
	}
	cout<<sp<<" ";
	cout<<minBack;
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值