【PAT甲级】个人做题记录之:1018 Public Bike Management (30 分)

PAT甲级圆满考完了,这次也考了一个相对不错的成绩,之后可能就很少刷PAT题库了,慢慢将之前刷题的过程补充记录上来吧

1018 Public Bike Management

题目描述

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.
【PAT甲级】个人做题记录之:1018 Public Bike Management (30 分)_第1张图片

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; 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 Sirespectively. 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

思路:

本题考察的是递归的应用,即更为复杂的DFS,需要用到剪枝与回溯。

对于这类问题,建议使用全局变量存储一些关键性数据,如下:

int map[MAXN][MAXN];//存储地图信息
int C,N,M,D;
int bike[MAXN];//存储自行车信息
bool visit[MAXN];//储存是否拜访过
vector pathtep,path;//临时存储路径,与最后储存路径
int mindis=INF;//储存最短路径长度
int need=0,back=0;

之后将数据输入,本人习惯写一个input函数,用于输入数据:

关键点:因为要求从起点开始运送自行车,将路过的每一个点的自行车补充至perfect,所以在最开始,记录每一个点缺少多少自行车(例如,C=10,某一点i自行车数量为3,则记录该点bike[i]=3-(10)/2=-2)这样做的目的是为了之后DFS的时候,可以直接对这个数进行处理

void input()
{
	scanf("%d %d %d %d",&C,&N,&D,&M);
	for(int i=1;i<=N;i++)
	{
		scanf("%d",&bike[i]);
		bike[i]=bike[i]-C/2;
	}
	for(int i=0;i<=N;i++)
		visit[i]=false;
	for(int i=0;i<=N;i++)
		for(int j=0;j<=N;j++)
			map[i][j]=INF;
	for(int i=0;i<M;i++)
	{
		int p1,p2,dis;
		scanf("%d %d %d",&p1,&p2,&dis);
		map[p1][p2]=map[p2][p1]=dis;
	}
	return;
}

核心DFS函数:

find_path(int p,int des,int dis,int cur,int cnt)
//在这里,需要传入五个参数,第一个为当前DFS的位置p,第二个为目标点des,第三个为当前走过的距离dis,第四个为记录当前的自行车个数(有可能为负数,证明次数需要这么多自行车),第五个根据目前的路径,需要的自行车个数

另外,注意怎么传入下一次递归的相应参数

void find_path(int p,int des,int dis,int cur,int cnt)
{
	visit[p]=true;
	pathtep.push_back(p);//将该点存入路径
	if(dis>mindis)//如果路径长度超出最大值
	{
	//回溯
		visit[p]=false;
		pathtep.pop_back();
		return;
	}
	if(p==des)//如果到达终点
	{
		if(dis<mindis)//如果距离更短,一定更新
		{
			mindis=dis;
			path=pathtep;
			//最终需要的自行车个数为cnt
			need=cnt;
			//最终要带回去的自行车个数为cur+need
			back=cur+need;
		}
		else if(dis==mindis)
		{
			if(cnt<need)//如果距离一样,需要发出的车更少,更新
			{
				mindis=dis;
				path=pathtep;
				need=cnt;
				back=cur+need;
			}
			else if(cnt==need)
			{
				if(cur+need<back)//距离一样,发出的车一样,回收的车更少,也更新
				{
					mindis=dis;
					path=pathtep;
					need=cnt;
					back=cur+need;
				}
			}
		}
		//回溯
		visit[p]=false;
		pathtep.pop_back();
		return;
	}
	for(int i=0;i<=N;i++)
	{
		if(map[p][i]!=INF&&!visit[i])
		{
			int nextdis=dis+map[p][i];
			//注意如何计算下一个节点的后需要的自行车
			int nextcur=cur+bike[i];//首先记录增加下一个节点后拥有的自行车个数
			int nextcnt=cnt;//记录增加了下一个节点需要的自行车,假设不变
			if(nextcur<-1*cnt)//如果下一节点拥有的自行车个数比需要的总共需要的自行车个个数还小,更新需要的自行车
				nextcnt=-1*nextcur;
			find_path(i,des,nextdis,nextcur,nextcnt);
		}
	}
	visit[p]=false;
	pathtep.pop_back();
	return;
}

主函数:
递归函数的入参为find_path(0,D,0,0,0),表示从当前从0节点开始,到D节点,初始距离为0,初始自行车为0,目前根据走过的路径,需要的自行车个数为0

int main()
{
	input();
	find_path(0,D,0,0,0);
	printf("%d ",need);
	for(int i=0;i<path.size();i++)
	{
		if(i!=0)
		{
			printf("->%d",path[i]);
		}
		else
		{
			printf("%d",path[i]);
		}
	}
	printf(" %d\n",back);
	return 0;
}

代码:

//1018 Public Bike Management
#include
#include

#define MAXN 501
#define INF 0x3f3f3f3f

using namespace std;

int map[MAXN][MAXN];//存储地图信息
int C,N,M,D;
int bike[MAXN];//存储自行车信息
bool visit[MAXN];//储存是否拜访过
vector<int> pathtep,path;//临时存储路径,与最后储存路径
int mindis=INF;//储存最短路径长度
int need=0,back=0;

void input();
void find_path(int p,int des,int dis,int cur,int cnt);
//递归解决查找最短路,p是当前的位置,des是终点,dis是当前路径长度
//cur是当前携带的自行车数量,cnt是当前需要携带的自行车个数

int main()
{
	input();
	find_path(0,D,0,0,0);
	printf("%d ",need);
	for(int i=0;i<path.size();i++)
	{
		if(i!=0)
		{
			printf("->%d",path[i]);
		}
		else
		{
			printf("%d",path[i]);
		}
	}
	printf(" %d\n",back);
	return 0;
}

void input()
{
	scanf("%d %d %d %d",&C,&N,&D,&M);
	for(int i=1;i<=N;i++)
	{
		scanf("%d",&bike[i]);
		bike[i]=bike[i]-C/2;
	}
	for(int i=0;i<=N;i++)
		visit[i]=false;
	for(int i=0;i<=N;i++)
		for(int j=0;j<=N;j++)
			map[i][j]=INF;
	for(int i=0;i<M;i++)
	{
		int p1,p2,dis;
		scanf("%d %d %d",&p1,&p2,&dis);
		map[p1][p2]=map[p2][p1]=dis;
	}
	return;
}

void find_path(int p,int des,int dis,int cur,int cnt)
{
	visit[p]=true;
	pathtep.push_back(p);//将该点存入路径
	if(dis>mindis)//如果路径长度超出最大值
	{
		visit[p]=false;
		pathtep.pop_back();
		return;
	}
	if(p==des)//如果到达终点
	{
		if(dis<mindis)//如果距离更短,一定更新
		{
			mindis=dis;
			path=pathtep;
			need=cnt;
			back=cur+need;
		}
		else if(dis==mindis)
		{
			if(cnt<need)//如果距离一样,需要发出的车更少,更新
			{
				mindis=dis;
				path=pathtep;
				need=cnt;
				back=cur+need;
			}
			else if(cnt==need)
			{
				if(cur+need<back)//距离一样,发出的车一样,回收的车更少,也更新
				{
					mindis=dis;
					path=pathtep;
					need=cnt;
					back=cur+need;
				}
			}
		}
		//回溯
		visit[p]=false;
		pathtep.pop_back();
		return;
	}
	for(int i=0;i<=N;i++)
	{
		if(map[p][i]!=INF&&!visit[i])
		{
			int nextdis=dis+map[p][i];
			//注意如何计算下一个节点的后需要的自行车
			int nextcur=cur+bike[i];//首先记录增加下一个节点后拥有的自行车个数
			int nextcnt=cnt;//记录增加了下一个节点需要的自行车,假设不变
			if(nextcur<-1*cnt)//如果下一节点拥有的自行车个数比需要的总共需要的自行车个个数还小,更新需要的自行车
				nextcnt=-1*nextcur;
			find_path(i,des,nextdis,nextcur,nextcnt);
		}
	}
	visit[p]=false;
	pathtep.pop_back();
	return;
}

你可能感兴趣的:(PAT考试)