PAT A1018:Public Bike Management之最短路径和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 S3, we have 2 different shortest paths:
PBMC -> S1 -> S3 .
In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3 , so that both stations will be in perfect conditions.

PBMC -> S2 -> S3 .
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: Cmax (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; Sp , 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 Ci(i=1,…,N) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si , Sj , and Tij which describe the time Tij taken to move betwen stations Si and Sj . 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->S1->…->Sp . Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp 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

求解思路

  1. 给定每个站点的容量Cmax、站点的数量N(每个站点标号为1~N,0表示PBMC)、出现问题的站点Sp、路径的数目M;给定每个站点当前的自行车数目、路径的源节点、目标结点以及该路径上的开销。
    要求我们选择一条从PBMC到达目标结点Sp的最短路径,如果出现多条路径,则选择从PBMC调出自行车数目最小的那条路径。输出选择的路径下必须从PBMC要调出的自行车数目和最短路径以及要调入PBMC的最小自行车数目。
  2. 用一个邻接表edge[i][j]来存储不同结点之间的路径关系,用一个一维数组dist[i]存储从PBMC到达不同结点的最短路径值,用一个一维数组vis[i]用于表示该结点是否已经被访问过,用一个一维数组curr[i]表示当前结点的自行车数目,用一个vector的二维数组pre来存储当前结点i的最短路径上的前驱结点,用一个vector的数组temp用于存储临时路径,一个vector数组path用于存储最优路径
  3. 最短路径:每次从未被访问过的结点中,选择与其直接相连的结点中距离最小的那个结点进行访问。对所有的结点进行判断是否需要更新其距离,如果需要,在这种情况下需要修改该结点的前驱结点。
  4. 先将目标结点加入到临时路径数组中,然后每次选择一个与目标结点直接相连的结点进行DFS遍历,并将其加入临时路径。当到达PBMC之后从PBMC开始统计在该路径上PBMC至少需要调出多少自行车以及调回多少自行车。
  5. 用need、out分别表示需要的补给和可以提供的供给,如果当前城市的curr为正数表示可以提供自行车,即out的值再加上curr;如果当前城市的curr为负数表示需要调入自行车,若此时out的值大于curr的绝对值,那么无需从PBMC调入车辆,否则需要从PBMC调入不够的部分车辆并且out的值变为零

代码(AC)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#define INF 1e9
const int maxn=501;
using namespace std;
int cmax,n,sp,m;	//分别用于表示每个站点的最大容量、站点的数量、目标站点、路径的数目
int edge[maxn][maxn];	//用于存储站点之间的关系
int curr[maxn];	//用于存储站点当前的自行车数量
int vis[maxn];	//用于标志当前结点是否被访问过
int dist[maxn];	//用于存储从PBMC到达当前结点的最短路径值
vector<int>pre[maxn];	//用于存储结点i的所有最短的路径中的前驱节点
vector<int>temp,path;	//分别用于存储当前路径和最优路径 
int min_need=INF,min_back=INF;	//PBCM调出、调入 
void init()
{
	/*首先对数组进行初始化*/
	fill(edge[0],edge[0]+maxn*maxn,INF);
	fill(curr,curr+maxn,0);
	fill(vis,vis+maxn,0);
	fill(dist,dist+maxn,INF);
	scanf("%d %d %d %d",&cmax,&n,&sp,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&curr[i]);
		curr[i]-=cmax/2;//表示为了获得平衡的话该结点需要调出或调入多少自行车,正为出,负为入 
	}	
	for(int i=0;i<m;i++)
	{
		int sr,goal,weight;
		scanf("%d %d %d",&sr,&goal,&weight);
		edge[sr][goal]=weight;
		edge[goal][sr]=weight;
	}
}
void Dijkstra(int source)
{
	dist[source]=0;
	for(int i=0;i<n;i++)
	{
		/*找距离源节点的最近的结点*/ 
		int u=-1,min_d=INF;
		for(int j=0;j<=n;j++)
		{
			if(vis[j]==0&&dist[j]<min_d)
			{
				u=j;
				min_d=dist[j];
			}
		}
		if(u==-1)	return;
		vis[u]=1; 
		/*更新距离*/
		for(int j=0;j<=n;j++)
		{
			if(vis[j]==0&&edge[u][j]!=INF)
			{
				if(dist[u]+edge[u][j]<dist[j])
				{
					pre[j].clear();
					dist[j]=dist[u]+edge[u][j];
					pre[j].push_back(u);
				}
				else if(dist[u]+edge[u][j]==dist[j])
				{
					pre[j].push_back(u);
				}
			}
		}
	}
}
void DFS(int dest)
{
	temp.push_back(dest);
	for(int i=0;i<pre[dest].size();i++)
		DFS(pre[dest][i]);
	temp.pop_back();
	if(dest==0)
	{
		temp.push_back(0);
		int need=0,out=0;	//需要补给、可以供给 
		for(int i=temp.size()-1;i>=0;i--)
		{
			if(curr[temp[i]]>0)	//表示可以供给 
				out+=curr[temp[i]];
			else	//需要补给 
			{
				if(out>abs(curr[temp[i]]))	//如果当前可供给大于需要补给 
				{
					out-=abs(curr[temp[i]]);
				}
				else
				{
					need+=abs(curr[temp[i]])-out;
					out=0;
				}
			} 
		}
		if(need<min_need)	//更新路径
		{
			min_need=need;
			min_back=out;
			path=temp;	
		} 
		else if(need==min_need&&out<min_back)
		{
			path=temp;
			min_back=out;
		}
		temp.pop_back();
		return;
	}
}
void solve()
{
	init();
	Dijkstra(0);
	DFS(sp);
	printf("%d ",min_need);
	for(int i=path.size()-1;i>=0;i--)
	{
		printf("%d",path[i]);
		if(i!=0)	printf("->");
	}
	printf(" %d",min_back);
}
int main()
{
	solve();
	return 0;	
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值