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.
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:
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.
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; S
p
, 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 C
i
(i=1,⋯,N) where each C
i
is the current number of bikes at S
i
respectively. Then M lines follow, each contains 3 numbers: S
i
, S
j
, and T
ij
which describe the time T
ij
taken to move betwen stations S
i
and S
j
. 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−>S
1
−>⋯−>S
p
. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S
p
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
题目分析
station半满状态下是最好的
若满或者空,PBMC要调整station到半满
选最短的路,若一样长,选需要携带最少bikes的路
输入:
C 最大容量 <=100
N statoin数量 <=500
Sp 有问题的station的下标 1——N
M road数量
Ci 各个station的当前情况
M行
Si Sj Tij 从Si到Sj要花费的时间 Tij
输出
要携带的bikes数
输出路径
要带回的bikes数
题目分析
station半满状态下是最好的
若满或者空,PBMC要调整station到半满
选最短的路,若一样长,选需要携带最少bikes的路
输入:
C 最大容量 <=100
N statoin数量 <=500
Sp 有问题的station的下标 1——N
M road数量
Ci 各个station的当前情况
M行
Si Sj Tij 从Si到Sj要花费的时间 Tij
输出
要携带的bikes数
输出路径
要带回的bikes数
题目关键点
关键点1:从路径到目的地,一路上的station全部都要调整为半满状态;
关键点2:从路径到目的地,一遍就需要将所有station都调整为版满状态,不存在回头继续调整;
故此题需要得到最短路径后,计算出按该路径走要携带的bikes数量和要带回的bikes数量,再与最少的比较,得到最佳答案;
不存在最佳子问题;
故不能用贪心算法dijkstra中添加条件得到最佳答案;
需要DFS得到路径后,计算sendbikes数量和backbikes数量比较;
难点在于上述判断后运用正确算法,再者就是如何在得到路径后计算sendbikes和backbikes数量;
1.输入操作
将到达不同车站的时间记录在Graph中;
将不同车站当前含有的车辆数放在Cur中;
#include<iostream>
#include<algorithm>
#include<vector>
#define inf 0x3fffffff
using namespace std;
int C,N,Sp,M;
int Cur[501];
int Graph[501][501];
void input()
{
fill(Graph[0],Graph[0]+501*501,inf);
cin>>C>>N>>Sp>>M;
Cur[0]=0;
for(int i=1;i<=N;i++)
{
cin>>Cur[i];
}
for(int i=0;i<M;i++)
{
int a,b,time;
cin>>a>>b>>time;
Graph[a][b]=time;
Graph[b][a]=time;
}
}
2.写一个DFS函数得到最佳路径,并比较
void DFS(int S,int Time) //初始为S,0,0
{
if(MinTime<Time) return; //不为最短路径,直接return
if(S==Sp) //到达终点
{
int send = 0, back = 0, bikeSum = 0;
for(int i=0;i<path.size();i++)
{
bikeSum+=Cur[path[i]];
send=max(send,(C/2)*i-bikeSum);
} //send已求出
back=send+bikeSum-(path.size()-1)*(C/2);
if(Time<MinTime){
MinSend=send;
MinBring=back;
Repath=path;
MinTime=Time;
return;
}
else if(Time == MinTime)
{
if(send<MinSend){
MinSend=send;
MinBring=back;
Repath=path;
return;
}
else if(send==MinSend&&back<MinBring)
{
MinBring=back;
Repath=path;
return;
}
}
}
for(int i=0;i<=N;i++)
{
if(Graph[S][i]!=inf&&!visited[i]) //说明有边
{
path.push_back(i); //保存路径
visited[i]=true;
DFS(i,Time+Graph[S][i]);
visited[i]=false;
path.pop_back();
}
}
return;
}
DFS的关键点在于计算出本次路径要带的bikes数量和要拿走的bikes数量;
int send = 0, back = 0, bikeSum = 0;
for(int i=0;i<path.size();i++)
{
bikeSum+=Cur[path[i]];
send=max(send,(C/2)*i-bikeSum);
} //send已求出
back=send+bikeSum-(path.size()-1)*(C/2);
遍历路径,得到路径上站点的总bikes数;
同时,从起始点要携带的bikes数应为路径中要填补的最大的自行车数;多余C/2的可以拿着给后面少的,(C/2)*i-bikeSum即为到当前结点位置要携带的自行车数,但是前面结点可能需要更多的自行车,故两者取大值;
send=max(send,(C/2)*i-bikeSum);
最后要带回的自行车数即为一开始携带的数量加上总的车数减去完美分配后的数量;
得到当前路径的send和back数量后,与最佳的比较,赋值即可;
3.main函数
初始化DFS的一些变量;
输出路径即可;
int main()
{
input();
fill(visited,visited+501,false);
visited[0]=true;
path.push_back(0);
DFS(0,0);
cout<<MinSend<<" ";
for(int i=0;i<Repath.size()-1;i++)
cout<<Repath[i]<<"->";
cout<<Repath[Repath.size()-1]<<" ";
cout<<MinBring;
}
注意点
path[]保存的路径输出时需要堆栈配合,反向输出;
vector保存的路径只要从头到尾输出即可,push_back和pop_back;
错误1:
用Dijkestra算法,一路过来都选携带bikes最少的,错误分析题目所需的算法。
/*
station半满状态下是最好的
若满或者空,PBMC要调整station到半满
选最短的路,若一样长,选需要携带最少bikes的路
C 最大容量 <=100
N statoin数量 <=500
Sp 有问题的station的下标 1——N
M road数量
Ci 各个station的当前情况
M行
Si Sj Tij 从Si到Sj要花费的时间 Tij
输出
要携带的bikes数
输出路径
要带回的bikes数
*/
#include<iostream>
#define inf 0x3fffffff
#include<stack>
using namespace std;
int C,N,Sp,M;
int Cur[501];
int Graph[501][501];
void input()
{
cin>>C>>N>>Sp>>M;
Cur[0]=0;
for(int i=1;i<=N;i++)
{
cin>>Cur[i];
}
for(int i=0;i<M;i++)
{
int a,b,time;
cin>>a>>b>>time;
Graph[a][b]=time;
Graph[b][a]=time;
}
}
//Graph0代表出发点
bool visited[501];
int Time[501]; //要花费的最短时间
int path[501]; //记录路径
int Cnt[501]; //计算要通过的顶点数
int SendBike(int i);
void Dijkstra()
{
for(int i=0;i<=N;i++) visited[i]=false;
for(int i=0;i<=N;i++) Time[i]=inf;
for(int i=0;i<=N;i++) path[i]=0;
for(int i=0;i<=N;i++) Cnt[i]=0;
//从0开始
Time[0]=0; //自己到自己时间为0
path[0]=-1; //路径设为-1
Cnt[0]=0;
while(1){
//找time里的最最小值
int Mint=inf;
int V=-1;
for(int i=0;i<=N;i++)
{
if(!visited[i]&&Time[i]<Mint)
{
Mint=Time[i];
V=i;
}
}
//找到最小值
if(V==-1) break;
visited[V]=true;
for(int i=0;i<=N;i++)
{
if(Time[V]+Graph[V][i]<Time[i]) //找最短路径
{
Time[i]=Graph[V][i]+Time[V];
path[i]=V;
Cnt[i]=Cnt[V]+1;
}
else if(Time[V]+Graph[V][i]==Time[i])
{
//选择要携带bike更小的路径
//此时终点为i
int A = Cnt[i]*C/2-SendBike(i); //这里是没有算V的车数 A
int B = (Cnt[V]+1)*C/2-(SendBike(i)+Cur[V]); //算上V结点后的要携带的自行车数
if(A>0&&B<A||A<0&&B<0&&B>A) //算上V后要带的车数少
//正数为要带的车数,负数为要拿回的车数
{
Time[i]=Graph[V][i]+Time[V];
path[i]=V;
Cnt[i]=Cnt[V]+1;
}
}
}
}
}
int SendBike(int i) //返回当前路径上的自行车总数
{
//Cnt中得到此路径要经过的顶点数;
//Cnt[Sp]*5/2即为总自行车数
//需要根据path来得到当前该路径上已有的总自行车数
int curbike=0;
while(path[i]!=-1){
curbike+=Cur[i];
i=path[i];
}
return curbike;
}
void Path(int n)
{
stack<int> P;
int i=n;
//P.push(n);
while(path[n]!=-1)
{
n=path[n];
P.push(n);
}
while(!P.empty())
{
int C=P.top();
P.pop();
cout<<C<<"->";
}
cout<<i<<" ";
}
int main()
{
input();
Dijkstra();
int SUMBK=Cnt[Sp]*C/2;
int CURBIKE=SendBike(Sp);
int SDBK=SUMBK-CURBIKE;
if(SDBK<0) SDBK=0;
int BRBIKE=CURBIKE-SUMBK;
if (BRBIKE<0) BRBIKE=0;
cout<<SDBK<<" ";
Path(Sp);
cout<<BRBIKE;
}
错误2:
计算send和back时,只将路径结点的车总量与最佳总量比较;
该方法计算出的结果应该是来回一遍,需要携带或者拿回的车;
但是题目只有过去的一趟,此次为题意理解错误;
#include<iostream>
#include<algorithm>
#include<vector>
#define inf 0x3fffffff
using namespace std;
//问题是只走过去那一趟——所以并能将总的数量算,因为只有过去的路能放自行车或者收自行车;
//!只走过去那一趟
int C,N,Sp,M;
int Cur[501];
int Graph[501][501];
void input()
{
fill(Graph[0],Graph[0]+501*501,inf);
cin>>C>>N>>Sp>>M;
Cur[0]=0;
for(int i=1;i<=N;i++)
{
cin>>Cur[i];
}
for(int i=0;i<M;i++)
{
int a,b,time;
cin>>a>>b>>time;
Graph[a][b]=time;
Graph[b][a]=time;
}
}
//Graph0代表出发点
bool visited[501];
vector<int> path; //记录路径
vector<int> Repath;
int MinTime=inf; //最短时间
int MinBikes=inf; //最少要携带的车
int MinBikes2=inf;
int SendBike(int i);
void DFS(int S,int Time,int CT) //初始为S,0,0
{
if(MinTime<Time) return; //不为路径最短
if(S==Sp) { //找到终点
//要判断携带的车数
int curbikes=0;
for(int i=0;i<path.size();i++){
curbikes+=Cur[path[i]];
}
int Temp = CT*C/2-curbikes; //这里是没有算V的车数 A
if(MinTime==Time){
if(Temp<MinBikes){ //要拿的车更小
if(Temp<0)
{
if(-Temp<MinBikes2){ //逻辑漏洞3 当temp<0时要判断br的数值,再确定是否改变bikes2
MinBikes=0;
MinBikes2=-Temp;
Repath=path;
}
}
else {
MinBikes=Temp;
Repath=path;
}
}
if(Temp==0){
MinBikes=0;
MinBikes2=0;
Repath=path;
}
}
else {
MinTime=Time;
if(Temp>=0) //说明要带车
{
MinBikes=Temp;
}
else{ //说明要拿车回来
MinBikes=0;
MinBikes2=-Temp;
}
Repath=path; //逻辑错误2,这个要写在里面
}
return;
}
for(int i=0;i<=N;i++)
{
if(Graph[S][i]!=inf&&!visited[i]) //说明有边
{
path.push_back(i); //保存路径
visited[i]=true;
DFS(i,Time+Graph[S][i],CT+1);
visited[i]=false;
path.pop_back();
}
}
return;
}
int main()
{
input();
fill(visited,visited+501,false);
visited[0]=true;
path.push_back(0);
DFS(0,0,0);
if(MinBikes>0) MinBikes2=0;
if(MinBikes2>0) MinBikes=0;
cout<<MinBikes<<" ";
for(int i=0;i<Repath.size()-1;i++)
cout<<Repath[i]<<"->";
cout<<Repath[Repath.size()-1]<<" ";
cout<<MinBikes2;
}