PAT (Advanced Level) 1018. Public Bike Management (30) 杭州自行车 最短路径+DFS

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.


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
使用Dijkstra算法求最短路径。
一开始误解了题意,以为从最短路径到达终点后,可以原路返回将多余的车补到先前缺车的车站。于是对每个车站添加了totalBike和totalStation两个参数,用来表征最短路径上平均车数,选出平均车数最短的一条路径。代码如下第一部分,case5、case6、case7失败。
在感觉自己写的代码没问题后,只好上网搜搜看是不是对题目的理解问题,果然,有人说不能原路返回,比如0->1->2;1号车站有1辆车,2号车站有9辆车,我们实际上还是要带4辆车出去补给给1号车站的。
于是,去掉了totalBike和totalStation两个参数,每个车站只要记录其车站序号,当前车数,以及到总站的最短路径即可,使用一个二维数组存储每个车站的前驱车站,dfs出所有最短路径。对每一条路径假设0补给出发,看缺多少车,带回多少车。比较得出最优路径。
假设可以原路返回的代码,不能AC。
/*2015.7.21cyq*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

const int MAX=2147483647;
//Dijkstra最短路径
struct station{
	int num;		//车站序号
	int bike;		//当前车数
	int length;		//到总站的总路程
	double totalBike;  //到总站的总车数
	double totalStation; //到总站的总车站数
	station(int n,int b,int l,double tb,double ts):num(n),bike(b),length(l),
				totalBike(tb),totalStation(ts){}
	bool operator < (const station &a)const{
		if(length<a.length)
			return true;
		if(length==a.length&&totalBike/totalStation>a.totalBike/a.totalStation)
			return true;//路程相等时,平均车数多的路线优先
		return false;
	}
};

bool cmpByNum(const station &a,const station &b){
	return a.num<b.num;
}
int main(){
	int Cmax,N,Sp,M;
	cin>>Cmax>>N>>Sp>>M;
	vector<vector<int> > roads(N+1,vector<int>(N+1,-1));//-1表示没有路
	vector<station> AB;//确定集合
	vector<station> UD;//未确定集合
	int x;
	for(int i=1;i<=N;i++){
		cin>>x;
		UD.push_back(station(i,x,MAX,0,0));//到总站距离为MAX,总车数为0,总车站数为1
	}
	int a,b,c;
	for(int i=1;i<=M;i++){
		cin>>a>>b>>c;
		roads[a][b]=c;
		roads[b][a]=c;
	}
	vector<int> preStation(N+1);
	preStation[0]=-1;
	station cur(0,0,0,0,0);//当前车站,初始化为总站
	AB.push_back(cur);	//已确定的集合
	while(!UD.empty()){//用cur对未确定集合进行修正
		for(auto it=UD.begin();it!=UD.end();it++){
			if(roads[cur.num][(*it).num]>0){//有路
				int tmpL=cur.length+roads[cur.num][(*it).num];
				if(tmpL<(*it).length){
					(*it).length=tmpL;
					(*it).totalBike=cur.totalBike+(*it).bike;
					(*it).totalStation=cur.totalStation+1;
					preStation[(*it).num]=cur.num;
				}else if(tmpL==(*it).length){//路程相等时,平均车数多的优先
					int tmpB=cur.totalBike+(*it).bike;
					if(tmpB/(cur.totalStation+1)> (*it).totalBike/(*it).totalStation){
						(*it).totalBike=cur.totalBike+(*it).bike;
						(*it).totalStation=cur.totalStation+1;
						preStation[(*it).num]=cur.num;
					}
				}
			}
		}
		sort(UD.begin(),UD.end());
		cur=UD[0];
		UD.erase(UD.begin());
		AB.push_back(cur);//已确定集合
	}
	sort(AB.begin(),AB.end(),cmpByNum);
	int k=Sp;
	int bikeToBeSend=(AB[k].totalStation*Cmax/2)-AB[k].totalBike;
	if(bikeToBeSend>0)
		cout<<bikeToBeSend;
	else
		cout<<0;
	//输出路径
	vector<int> path;
	while(preStation[k]!=-1){
		path.push_back(k);
		k=preStation[k];
	}
	cout<<" 0";
	for(auto it=path.rbegin();it!=path.rend();it++)
		cout<<"->"<<(*it);
	cout<<" ";

	if(bikeToBeSend>0)
		cout<<0;
	else
		cout<<-bikeToBeSend;
	return 0;
}
不能原路返回的代码,AC。
/*2015.7.21cyq*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

//ifstream fin("case2.txt");
//#define cin fin

const int MAX=2147483647;

struct station{
	int num;		//车站序号
	int bike;		//当前车数
	int length;		//到总站的总路程
	station(int n,int b,int l):num(n),bike(b),length(l){}
	bool operator < (const station &a)const{
		return length<a.length;
	}
};

bool cmpByNum(const station &a,const station &b){
	return a.num<b.num;
}
//利用前驱数组dfs出所有路径,逆序存储,总站为终点
void dfs(vector<vector<int> > &a,int &start,int &end,vector<int> &path,vector<vector<int> > &result){
	path.push_back(start);
	if(start==end){
		result.push_back(path);
	}else{
		for(auto it=a[start].begin();it!=a[start].end();++it){
			dfs(a,(*it),end,path,result);
		}
	}
	path.pop_back();
}

int main(){
	int Cmax,N,Sp,M;
	cin>>Cmax>>N>>Sp>>M;
	vector<vector<int> > roads(N+1,vector<int>(N+1,-1));//-1表示没有路
	vector<station> AB;//确定集合
	vector<station> UD;//未确定集合
	int x;
	for(int i=1;i<=N;i++){
		cin>>x;
		UD.push_back(station(i,x,MAX));//到总站距离为MAX,
	}
	int a,b,c;
	for(int i=1;i<=M;i++){
		cin>>a>>b>>c;
		roads[a][b]=c;
		roads[b][a]=c;
	}
	vector<vector<int> > preStation(N+1);
	station cur(0,0,0);//当前车站,初始化为总站
	AB.push_back(cur);	//已确定的集合
	while(!UD.empty()){//用cur对未确定集合进行修正
		for(auto it=UD.begin();it!=UD.end();it++){
			if(roads[cur.num][(*it).num]>0){//有路
				int tmpL=cur.length+roads[cur.num][(*it).num];
				if(tmpL<(*it).length){
					(*it).length=tmpL;
					preStation[(*it).num].clear();//注意清空之前的前驱数组
					preStation[(*it).num].push_back(cur.num);
				}else if(tmpL==(*it).length){
					preStation[(*it).num].push_back(cur.num);
				}
			}
		}
		sort(UD.begin(),UD.end());
		cur=UD[0];
		UD.erase(UD.begin());
		AB.push_back(cur);//已确定集合
	}
	sort(AB.begin(),AB.end(),cmpByNum);//AB[i].length即为车站i到总站的最短路径

	int PBMC=0;//总站
	vector<int> path;
	vector<vector<int> > result;//dfs出总站到Sp的所有最短路径,逆序存储
	dfs(preStation,Sp,PBMC,path,result);

	vector<int> bestPath;
	int minBike=2147483647;
	int bikeReturn=0;
	for(auto it=result.begin();it!=result.end();++it){
		int curBike=0;  //假设0补给出发,看缺多少车
		int bikeNeed=0;
		auto itt=(*it).rbegin()+1;
		while(itt!=(*it).rend()){
			int tmp=curBike+AB[*itt].bike;//补给车数加上该站车数
			if(tmp<Cmax/2){//补给不够
				bikeNeed+=Cmax/2-tmp; 
				curBike=0;
			}else{
				curBike=tmp-Cmax/2;
			}
			itt++;
		}
		if(bikeNeed<minBike||(bikeNeed==minBike&&curBike<bikeReturn)){
			minBike=bikeNeed;
			bikeReturn=curBike;
			bestPath=(*it);
		}
	}
	cout<<minBike<<" 0";
	for(auto it=bestPath.rbegin()+1;it!=bestPath.rend();++it)
		cout<<"->"<<*it;
	cout<<" "<<bikeReturn;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值