题目描述:
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.
输入测试
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
输出结果
3 0->2->3 0
思路分析:
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
#define MAX 505
#define INF 10000
int cap,N,sp,M;
int vex;
int dist[MAX][MAX];
int bike[MAX];
#define PF cap/2
vector<int> curpath;
vector<int> shortpath;
int minsend=INF,minback=INF;
int minlen=INF;
int cursend=0,curback=0;
int curlen=0;
bool visit[MAX]={0};
void dfs(int cur){
if(curlen>minlen)
return;
if(cur==sp){
//到达目标点,看是否最优
if(curlen<minlen){
minlen=curlen;
minsend=cursend;
minback=curback;
shortpath=curpath;
}
else if(curlen==minlen){
if(cursend<minsend||(cursend==minsend&&curback<minback)){
minsend=cursend;
minback=curback;
shortpath=curpath;
}
}
return;
}
for(int i=1;i<vex;i++){
if(visit[i]==true||dist[cur][i]==INF)
continue;
int lastsend=cursend;
int lastback=curback;
//计算到达当前点的send和back数
if(bike[i]+curback<PF){
cursend+=PF-bike[i]-curback;
curback=0;
}
else{
curback=bike[i]+curback-PF;
}
visit[i]=true;
curpath.push_back(i);
curlen+=dist[cur][i];
dfs(i);
curpath.pop_back();
visit[i]=false;
curlen-=dist[cur][i];
cursend=lastsend;
curback=lastback;
}
}
int main(){
cin>>cap>>N>>sp>>M;
//初始化,距离置为INF
vex=N+1;
for(int i=0;i<vex;i++){
for(int j=0;j<vex;j++){
dist[i][j]=dist[j][i]=INF;
}
}
for(int i=1;i<vex;i++){
cin>>bike[i];
}
for(int k=0;k<M;k++){
int i,j;
cin>>i>>j;
cin>>dist[i][j];
dist[j][i]=dist[i][j];
}
dfs(0);
printf("%d 0",minsend);
for(int i=0;i<shortpath.size();i++){
printf("->%d",shortpath[i]);
}
printf(" %d",minback);
return 0;
}
杭州公共自行车调度算法
本文介绍了一种针对杭州公共自行车系统的调度算法。该算法通过寻找从调度中心到问题站点的最短路径,并在此基础上选择所需的最少自行车调度数量,确保各站点处于理想状态。文章提供了完整的C++代码实现。

1003

被折叠的 条评论
为什么被折叠?



