PATA 1018 Public Bike Management(30 分)解题报告

本文介绍了一个关于公共自行车管理的问题,系统的目标是保持每个站点的自行车数量达到其最大容量的一半。调度中心通过选择最短路径和最少自行车调动量来调整站点状态。文章详细阐述了解题思路,并提供了具体的解题方案,强调了在路径选择中需要考虑的多个因素,包括路径长度和自行车的调动数量。最后,给出了AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

 

题目大意:

在杭州有很多公共自行车,可以从任意车站骑走并归还到任意车站
我们称车站的perfect状态是单车数量恰好为车站容量的一半
如果有车站满或空,调度中心会派人去运送单车,使得车站状态达到perfect
同时,沿路上的车站状态也都会被调整为perfect
调度中心在选择调度路线时,会选择从调度中心到达目标车站路程最短的那条路
如果不止一条路程最短的路,调度中心就会选择搬运单车数量最少的路
如果带出单车数量最少的路还有多条,就选择带回数量最少的一条 
在一行内给出调度中心需要送出多少辆单车,送车的路径,以及需要送回多少辆车到调度中心

解题思路:

本题不能用直接的Dijkstra算法解决,因为minNeed和minRemain的传递不满足最优子结构
只有当所有的路径都确定后,才能去选择最优的路径
题目需要在从调度中心出发到达问题点sp的过程中就把路径上所有节点调整好,此后不再调整
在本题中,路径长短是第一标尺,带出单车数量是第二标尺,带回单车数量是第三标尺
因为是在路径上遇见一个调整一个节点,所以need和remain有时候会都为正值
比如第一个节点需要补10辆车,你就要把need+=10,第二个节点又要送出20辆,这时候就要把remain+=20
然后进入第三个节点进行处理

总的来说就是先用Dijkstra算法找最短路径,全部保存下来,然后在DFS遍历中检查第二和第三标尺,选择最优路径

 

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<cctype>
using namespace std;
const int maxn=600;
const int INF=0x3f3f3f3f;
bool vis[maxn];
int d[maxn];
int weight[maxn],w[maxn];
int G[maxn][maxn];
int c,n,sp,m;
int minNeed=INF,minRemain=INF;//最少携带和最少带回数目 
vector<int> pre[maxn];//前驱 
vector<int> tempPath,path;//临时路径和最优路径 
void Dijkstra(int s);
void DFS(int v);
int main()
{
	scanf("%d%d%d%d",&c,&n,&sp,&m);
	fill(G[0],G[0]+maxn*maxn,INF);
	for(int i=1;i<=n;i++)
	{
		//减去c/2,就可以直接从weight[i]的值来判断需要加入还是带走多少单车了 
		scanf("%d",&weight[i]);
		weight[i]-=c/2;
	}
	int u,v,dis;
	for(int i=0;i<m;i++)
	{ 
		scanf("%d%d%d",&u,&v,&dis);
		G[u][v]=dis;
		G[v][u]=dis;
	}
	Dijkstra(0);//算法起点从调度中心开始 
	DFS(sp);
	printf("%d ",minNeed);
	for(int i=path.size()-1;i>=0;i--)
	{
		printf("%d",path[i]);
		if(i>0) printf("->");
	}
	printf(" %d",minRemain);
	return 0;
}

void Dijkstra(int s)
{
	fill(d,d+maxn,INF);
	d[s]=0;
	for(int i=0;i<n;i++)
	{
		int u=-1,MIN=INF;
		for(int j=0;j<=n;j++)
		{
			if(vis[j]==false&&d[j]<MIN)
			{
				u=j;
				MIN=d[j];
			}
		}
		if(u==-1) return;
		vis[u]=true;
		for(int v=0;v<=n;v++)
		{
			if(vis[v]==false&&G[u][v]!=INF)
			{
				if(d[v]>d[u]+G[u][v])//找到更优路线,清空原有方案,记录新路线 
				{
					d[v]=d[u]+G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if(d[v]==d[u]+G[u][v])//相同优先级的路线,直接记录 
					pre[v].push_back(u);
			}
		}
	}
}

void DFS(int v)
{
	if(v==0)//递归边界,叶子节点 
	{
		tempPath.push_back(v);
		int need=0,remain=0;//从调度中心到当前车站必须携带的数目,从车站带回调度中心的数目 
		for(int i=tempPath.size()-1;i>=0;i--)//此处必须倒着枚举 
		{
			int id=tempPath[i];
			if(weight[id]>0)//点权>0,说明需要带走一部分自行车 
				remain+=weight[id];//当前自行车持有量+weight[id] 
			else
			{//weight为负数时代表需要补给自行车,数量为abs(weight),如果remain>0就可以用来补给 
				if(remain>abs(weight[id]))//remain足够用来补给当前车站 
					remain-=abs(weight[id]);//当前持有量减少补给的量 
				else//remain不够用来补给当前车站 
				{
					need+=abs(weight[id])-remain;//不够的不分从调度中心携带 
					remain=0;//remain全部用来调度之后,把remain置0 
				}
			}
		}
		if(need<minNeed)//如果从调度中心携带的单车数量变少 
		{
			minNeed=need;//更新minNeed 
			minRemain=remain;//覆盖minRemain 
			path=tempPath;//更新最优路径 
		}
		else if(need==minNeed&&remain<minRemain)//携带数目相同,带回数目变少 
		{
			minRemain=remain;
			path=tempPath;
		}
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(v);
	for(int i=0;i<pre[v].size();i++)
		DFS(pre[v][i]);
	tempPath.pop_back();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值