1018 Public Bike Management

题目来源:PAT (Advanced Level) Practice

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

words:

convenience 方便,遍历        monitor 监控,监测        adjust 调整        illustrate 说明,表明

inside 里面的,内部的        capacity 容量        sent 派送        

题意:

有n个点(每个点都有一个权值)和一个PBMC(编号为0)以及m条边组成的带权无向图,求源点PBMC到目的点sp的最短路径,若不唯一则需求根据点的权值选出一个条符合题目要求的,有点复杂;

思路:

1. 使用邻接矩阵存储带权无向图;

2. 使用Dijkstra算法求目的点到源点的所有最短路径;最短路径的求解中应该保存具有最短路径的所有前驱点

3. 使用DFS从目的点回溯到源点的所有最短路径,找出使得题目中PBMC发带出车子最少(若不唯一则带回车子最少)的路径,并输出;

//PAT ad 1018 Public Bike Management
#include <iostream> 
using namespace std;
#include <vector> 
#include <algorithm> 
#define N 505
#define maxInt 999999999

int adj[N][N];	//邻接矩阵
int current[N];	//当前每个点的车的数量 
int c,n; 

int dis[N];		//最短路径 
bool vis[N];	//访问标记 
vector<int> path[N];	//记录前驱,记录最短路径的所有前驱 


int Dijkstra(int v0)		//常规迪杰斯特拉算法 
{
	int i,v,w;

	for(v=0;v<=n;v++)
	{
		vis[v]=false;
		dis[v]=adj[v][v0];
		if(dis[v]<maxInt)
			path[v].push_back(v0);				//定前驱 					
	}
	vis[v0]=true;dis[v0]=0;
	for(i=0;i<=n-1;i++)
	{
		int mi=maxInt;
		for(w=0;w<n;w++)
			if(!vis[w]&&dis[w]<mi)	//找最小 
			{
				v=w;mi=dis[w];
			}
		vis[v]=true;
		for(w=0;w<=n;w++) 					//做更新 
			if(!vis[w]&&dis[v]+adj[v][w]<dis[w])	//间接距离小于直接距离 
			{
				dis[w]=dis[v]+adj[v][w];
				path[w].clear();path[w].push_back(v);	//修改w的前驱 
			}
			else if(!vis[w]&&dis[v]+adj[v][w]==dis[w])	//间接距离等于直接距离 
				path[w].push_back(v);					//增加w的前驱 						
	}
}

vector<int> tempPath,pa;	//零时路径,最终路径 
int minNeed=maxInt,minRemain=maxInt;	//最少需求和最小带回 

void DFS(int v)		//从目标点v回溯源点0,找所需最少(不唯一则带回最少的路径) 
{
	if(v==0)	//PBMC,即源点 
	{
		tempPath.push_back(v);	//放入临时路径中 
		int need=0,remain=0;	//初始化需求和剩余(带回) 
		for(int i=tempPath.size()-1;i>=0;i--)	//遍历(从源点到目的点)临时路劲中的所有点 
		{
			int id=tempPath[i];		//前驱点 
			if(current[id]>0)		//有多余 
				remain+= current[id] ;		//需带走
			else	//current[id]<0		有欠缺 
			{
				if(remain >abs(current[id])) 	//可弥补 
					remain-=abs(current[id]);
				else			//不够弥补 
				{
					need+=abs(current[id])-remain;
					remain=0;
				}
			 } 
		}
		
		if(need<minNeed)		//所需更少 
		{
			minNeed=need;
			minRemain=remain;
			pa=tempPath;
		}
		else if(need==minNeed&&remain<minRemain) 	//所需一样,带回更少
		{
			minRemain=remain;
			pa=tempPath; 
		}
		
		tempPath.pop_back();	//舍掉当前前驱点,增加新的前驱点 
		return ;
	}
	
	tempPath.push_back(v);		//将当前点放入临时路径中 
	for(int i=0;i<path[v].size();i++)	//找当前点的前驱 
		DFS(path[v][i]);				//从前驱点回溯源点 
	
	tempPath.pop_back();		//将当前点从临时路径中取出 

}


int main()
{
	int sp,m,i,j;
	cin>>c>>n>>sp>>m;
	
	for(i=1;i<=n;i++)		//当前每个点的车的数量
	{
		cin>>current[i];
		current[i]-=c/2;
	} 
		
		
	for(i=0;i<=n;i++)		//初始化邻接矩阵 
		for(j=i;j<=n;j++)
			adj[i][j]=adj[j][i]=maxInt;
			
	int a,b,w;		
	for(i=0;i<m;i++)		//输入边和权值 
	{
		cin>>a>>b>>w;
		adj[a][b]=adj[b][a]=w;
	}
	
	Dijkstra(0);		//迪杰斯特拉算法求最短路径
	DFS(sp);			//从目标点sp向源点0回溯路劲 
	
	cout<<minNeed<<" ";		//最少需求
	
	string ans; 
	for(auto w:pa)	ans=to_string(w)+"->"+ans; 		//最短路径 
	ans.pop_back();ans.pop_back();
	cout<<ans<<" ";
	
	cout<<minRemain<<endl;	//最少带回 
	
	
	
	
	return 0;
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值