1018. Public Bike Management (30)

本文介绍了一个关于公共自行车系统的问题解决方法,通过Dijkstra算法寻找最短路径,并结合深度优先搜索来确定最优的自行车调配方案,确保每个站点达到理想的使用状态。

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

题目要求:

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 perfectcondition 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.


Figure 1

Figure 1 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:

1. 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.

2. 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
分析:
这一题和1003很像,主要考察的还是dijkstra算法和dfs深搜,可能这一题只用深搜应该也可以实现,不过我想练习一下dijkstra算法,所以顺便也用了。
首先用dijkstra算法求出PBMC(0点)到目标点的最短距离。然后深搜穷举,每次深搜找到一条最短路径后依次记录路径上的各点,然后扫描,如果有的station少于最大容量的一半,带出的车数就加到take中;如果多了,带回去的车数加到back中。注意,每一站多的车辆只能放到后面一站去,而不能补充前面车站中少的数目。这是一个陷阱,我一开始没有注意到,结果后5个case一直过不去。如果前面车站有多的,那么优先将多出来的补充这一站缺少的。
最后就是优先级问题,第一是路径最短,第二是带出来的车数最少,第三是带回去的车数最少。
在每次dfs迭代中,利用前面的最短路径可以对dfs的递归进行剪枝,比如如果当前的路径长度已经超过了到目标点的最短路径,那么就直接return。

代码如下:
#include <stdio.h>

#define INF 10000000

int station[501];
int path[501][501];
int visit[501];
int dist[501];
int take,min_take,back,min_back,count,min_count,min_num,length,c_hf;
int pass[501],min_pass[501];


void init(int *v,int n);
void dijkstra(int s,int n);
int dfs(int b,int s,int n);

int main(int argc,char* argv[])
{
	int i,j,n,s,c,m,si,sj,t;

	//freopen("input","r",stdin);

	scanf("%d%d%d%d",&c,&n,&s,&m);

	c_hf = c / 2;

	for(i=1;i<=n;i++)
		scanf("%d",&station[i]);

	//init the path[][]
	for(i=0;i<=n;i++)
		for(j=0;j<=n;j++)
		{
			if(i == j)
				path[i][j] = 0;
			else
				path[i][j] = INF;
		}
	
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&si,&sj,&t);
		path[si][sj] = t;
		path[sj][si] = t;
	}

	dijkstra(0,n);
	/*for(i=0;i<=n;i++)
		printf("dist from 0 to %d: %d\n",i,dist[i]);*/
	
	length = count = 0;
	min_take = min_back = INF;
	init(visit,n);
	pass[0] = 0;
	dfs(0,s,n);

	printf("%d ",min_take);
	printf("0");
	for(i=1;i<=min_count;i++)
		printf("->%d",min_pass[i]);
	printf(" %d\n",min_back);

	return 0;
}

void init(int *v,int n)
{
	int i;
	for(i=0;i<=n;i++)
		visit[i] = 0;
}

void dijkstra(int s,int n)
{
	int i,j,x,min;

	init(visit,n);

	for(i=0;i<=n;i++)
		dist[i] = path[s][i];

	visit[s] = 1;
	for(i=1;i<=n;i++)
	{
		min = INF;
		for(j=1;j<=n;j++)
			if(!visit[j] && dist[j]<min)
			{
				min = dist[j];
				x = j;
			}
		visit[x] = 1;
		for(j=1;j<=n;j++)
		{
			if(!visit[j])
			{
				if(dist[j] > dist[x]+path[x][j])
					dist[j] = dist[x]+path[x][j];
			}
		}
	}
}

int dfs(int b,int s,int n)
{
	int i;

	if(length > dist[s])
		return ;

	if(b == s)
	{
		take = back = 0;
		for(i=1;i<=count;i++)
		{
			if(station[pass[i]] < 5)
			{
				if( back >= (c_hf - station[pass[i]]) )
					back -= c_hf - station[pass[i]];
				else
				{
					take += c_hf-station[pass[i]]-back;
					back = 0;
				}

			}
			else if(station[pass[i]] > 5)
				back += station[pass[i]] - c_hf;
		}

		if(take<min_take || (take==min_take&&back<min_back) )		
		{
			min_take = take;
			min_back = back;
			min_count = count;
			for(i=0;i<=count;i++)
				min_pass[i] = pass[i];
		}

		return ;
	}


	for(i=1;i<=n;i++)
	{
		if(!visit[i] && path[b][i]!=INF)
		{
			length += path[b][i];
			pass[++count] = i;
			visit[i] = 1;
			dfs(i,s,n);
			visit[i] = 0;
			count --;
			length -= path[b][i];
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值