PAT Advanced—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:
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.
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

题目意思:
  一个自行车站,最完美的车辆数量是最大容量的一半。如果一个车站的车辆数为0或为满,则需要从调度中心携带相应的车辆,找到最短路径,并将这条路径上的车站都变成完美状态。有三个条件,在路径最短的前提下,要求从中心带出的车辆尽量少,存在同等情况时,选择带回车辆最少的方案。

思路:
  首先需要寻找最短路径,在最短路径的前提下,选择带出最少的路线,再相同,选择带回最少的路线。

注意:

  1. 求最短路径可以用Dijkstra算法或者DFS
  2. DFS算法的套路都是一样的,要有一个退出条件,参数比较多的时候,直接定义为全局变量操作
  3. 这道题的主要难点在于如何判断需要发送和带回的自行车数量

代码:(C++)

#include<iostream>
#include<vector>

using namespace std;

#define inf 999999
#define MAX 505

/*
 cmax:一个车站的最大车辆数,为偶数
 n:全部车站数量
 sp:出现问题的车站编号
 m:公路数量
 bike:各个车站的自行车数量
*/
int cmax, n, sp, m, send, back;

//最大自行车数量
int bike[MAX];
//路线信息
int route[MAX][MAX];
//是否访问过
int visit[MAX];
int minsend = inf, minback = inf, mindis = inf;
vector<int> path, temppath;

/*
 v:当前结点
 tempsend:从中心发出的自行车数
 tempback:带回的自行车数
 tempdis:当前路径的长短
*/
void dfs(int v, int tempsend, int tempback, int tempdis)
{
	temppath.push_back(v);  //存放当前路径中的结点
	visit[v] = 1;
	if(v == sp)  //到达了问题结点
	{
		if(mindis > tempdis)  //如果当前路线优于最小路线,则替换
		{
			minsend = tempsend;
			minback = tempback;
			mindis = tempdis;
			path = temppath;
		}
		else if(mindis == tempdis)  //这就是路线相同时候考虑的情况
		{
			if(minsend > tempsend)
			{
				minsend = tempsend;
				minback = tempback;
				path = temppath;
			}
			else if(minsend == tempsend && minback > tempback)
			{
				minback = tempback;
				path = temppath;
			}
		}
		return;
	}
	for(int i=1; i<=n; i++)  //遍历相邻结点
	{
		if(visit[i] == 0 && route[v][i] != inf)
		{
			int temp = bike[i] - cmax/2;  //每个站点车辆距完美容量的差值
			int tempb = tempback, temps = tempsend;  //发出和带回结点的车辆数
			if(temp >= 0)  //要带回的车辆增加
				tempb += temp;
			else
			{
				if(tempback + temp < 0)  //沿路带的车辆数少了,则需要在发送时增加
				{
					temps -= (tempback + temp);
					tempb = 0;
				}
				else
				{
					tempb += temp;  //否则带回数量增加
				}
			}
			dfs(i, temps, tempb, tempdis + route[v][i]);  //递归访问下一个结点
			temppath.pop_back();  //后退一个站点
			visit[i] = 0;  //又从temppath里最后一个点开始,查看可能路径
		}
	}
}

int main()
{
	scanf("%d %d %d %d",&cmax,&n,&sp,&m);
	fill(route[0],route[0] + 505*505, inf);  //先将路线都填充为最大,不可达
	for(int i=0; i<n; i++)
		scanf("%d", &bike[i+1]);  //输入各结点自行车数量
	for(int i=0; i<m; i++)
	{
		int ti,tj,v;
		scanf("%d %d %d", &ti,&tj,&v);
		route[ti][tj] = route[tj][ti] = v;  //填充路线
	}
	dfs(0,0,0,0);  //从中心开始寻找

	//输出结果
	printf("%d ", minsend);
	for(int i=0; i<path.size(); i++)
	{
		if(i != 0)
			printf("->");
		printf("%d", path[i]);
	}
	printf(" %d", minback);
	
	return 0;
}

参考:
  https://blog.csdn.net/lianwaiyuwusheng/article/details/87916550

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值