【PAT】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.

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

翻译:杭州的公共自行车为来自世界各地的游客提供了巨大的方便。游客可以在任意一个车站借自行车并在其他任意一个车站归还。公共自行车管理中心(PBMC)一直在监视各个车站的实时容量。当一个车站的车辆正好等于最大容量的一半时,就称为最佳状态。如果一个车站的车辆数是满的或是空的,则PBMC将会回收或送去一些自行车来使该车站到达最佳状态。还有,所有沿路的车站也会被调配到最佳状态。
当一个问题车站被上报了,PBMC总是会选择到达该车站的最短路。如果有多条最短路,则选择PBMC需要送的最少的路线。
拿图一举例。车站用点来表示,道路用边来表示。一条边上的数字代表从其一端的车站到另一端所花的时间。顶点S内的数字代表S的车辆数。给出的每个车站的最大容量为10。为了解决S3的问题,我们有两条不同的最短路。
1.PBMC->S1->S3,在这种情况下,PBMC需要带4辆自行车,因为我们可以从S1获得一辆多余的自行车,然后带5辆车去S3,这样两个车站都是最佳状态。
2.PBMC->S2->S3,这条路所花时间与S1一样,但是只需要带3辆自行车,所以最后会选择这条路。

INPUT FORMAT

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.

翻译:每个输入文件包含一组测试数据。每组测试数据第一行包括4个数字:Cmax(<=100),总是一个偶数,代表每个车站的最大容量。N,车站总数。Sp,问题车站的编号(车站被标记为1—N,PBMC为0)。M,道路的数量。第二行的N个非负数Ci代表编号i的车站的自行车数量。接下来的M行,每行包括3个数字:Si,Sj,Tij,Tij代表从Si到Sj所需要的时间。一行内所有数字都用空格隔开。

OUTPUT FORMAT

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.

翻译:对于每组测试数据,一行内输出你的结果。第一行输出PMBC所需要送的车辆数。空一格,接下来以以下格式输出所选的最短路: 0->S1->…->Sp。最后再空一格,输出我们调整好沿路车站后所要带回PBMC的车辆数。


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


解题思路:

这道题用dijkstra过不了,有一个点一直过不去。。。。原因是这道题并不是严格的取最小,比如同一个点,一个数据为送8辆,回8辆,一个数据为送9辆,回11辆。假如终点需要送12辆,则需要选择第二个数据,而dijkstra算法会选择第一个,因为在那一点它需要送的数量最少。所以我们可以用dfs搜索所有最短路,找出最优解。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
using namespace std;
struct station{
	int tour,bike; 
	station(){}
	station(int bike):bike(bike){
		tour=INF;
	}
};
struct edge{
	int to,length;
	edge(int a,int b):to(a),length(b){}
};
vector<edge>G[510];
station st[510];
int v[510];
vector<int>path;
vector<int>anspath;
int Sum=INF,sum=0;
int Bring=INF,bring=0;
int Take=INF,take=0;
int Cmax,N,Sp,M;
void dfs(int a){
	ccount++;
	if(sum>Sum)return ;
	if(a==Sp){//当为终点时判断是否为最优解 
		if(Sum>sum||(Sum==sum&&(Bring>bring||(Bring==bring&&Take>take)))){
			Sum=sum;
			Bring=bring;
			Take=take;
			anspath=path;
		}
		return ;
	}
	for(int i=0;i<G[a].size();i++){//矩阵据说也能过 
		int to=G[a][i].to,length=G[a][i].length;
		int tempbring=bring,temptake=take,tempsum=sum;//保存值,便于回溯 
		if(!v[to]&&st[to].tour>=st[a].tour+length){//如果不是距离最短直接跳过 
			v[to]=1;						 
			take+=st[to].bike;
			if(take<0)bring-=take,take=0;
			sum+=length;
			st[to].tour=st[a].tour+length;
			path.push_back(to);					   
			dfs(to);							  
			v[to]=0;								//回溯 
			bring=tempbring;
			take=temptake;
			sum=tempsum;
			path.pop_back();
		}
	}
}
int main(){
	scanf("%d%d%d%d",&Cmax,&N,&Sp,&M);
	Cmax/=2;
	int bike;
	st[0]=station(0);
	st[0].tour=0;
	for(int i=1;i<=N;i++){
		scanf("%d",&bike);
		st[i]=station(bike-Cmax); //bike为正则需带回,为负则需调配 
	}
	int a,b,c;
	for(int i=0;i<M;i++){
		scanf("%d%d%d",&a,&b,&c);
		G[a].push_back(edge(b,c));
		G[b].push_back(edge(a,c));
	}
	dfs(0);
	printf("%d 0",Bring);
	for(int i=0;i<anspath.size();i++){
		printf("->%d",anspath[i]);
	} 
	printf(" %d",Take);
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值