1018 Public Bike Management #
0、题目 #
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; 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 $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 between 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$−>$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
1、大致题意 #
从 0
点开始,找到 $S_p$ 的最短路,并使得路径上所有站点的自行车数量为 $\frac{C_{max}}{2}$。
- 若不唯一,找其中需要送出自行车最少的路径;+ 若还不唯一,找其中需要回收自行车最少的路径;
2、基本思路 #
dijkstra
求单源最短路,在求最短路的同时,记录所有的最短路径。然后按照上面的两个条件进行比较。
关于最短路的思路,在 1003 Emergency有写过,并且我也做过相关的总结 图论-最短路径问题。
3、解题过程 #
安利一个 在线代码比较网站。
3.1 第一份代码(22/30) #
有了上面的思路,很快写出了第一份代码
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=505;
const int maxm=501000;
int cmax,n,m,sp,c[maxn];
int head[maxn],vis[maxn],dis[maxn],num_edge;
int pre[maxn];
int num_ans;
vector<int>ans[maxn];
struct Edge {
int to,wight,next;
} edge[maxm];
void addEdge(int from, int to,int wight) { //头插法
edge[++num_edge].to=to;
edge[num_edge].wight=wight;
edge[num_edge].next=head[from];
head[from]=num_edge;
}
struct node { //到固定结点的距离
int dis;
int pos;
bool operator <( const node &x )const {
return x.dis < dis;
}
};
priority_queue<node>q;
void dijkstra(int sp) {
for(int i=1; i<=n; i++) {
dis[i]=inf;
vis[i]=0;
pre[i]=-1;
}
//从0点开始
q.push((node) {
0,0
});
dis[0]=0;
pre[0]=0;
while(!q.empty()) {
node u=q.top();
q.pop();
int pos=u.pos;
if(vis[pos]) {
continue;
}
vis[pos]=1;
for(int i=head[pos]; i; i=edge[i].next) {
int to=edge[i].to;
if(to==sp) {
if(dis[to]>=dis[pos]+edge[i].wight) {
if(dis[to]>dis[pos]+edge[i].wight) {
num_ans=0;
dis[to]=dis[pos]+edge[i].wight;
pre[to]=pos;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
ans[num_ans].clear();
for(int j=to; j; j=pre[j]) { //保存路径
ans[num_ans].push_back(j);
}
ans[num_ans].push_back(0);
num_ans++;
}
} else {
if(dis[to]>dis[pos]+edge[i].wight) {
dis[to]=dis[pos]+edge[i].wight;
pre[to]=pos;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
}
}
}
}
int main() {
cin>>cmax>>n>>sp>>m;
for(int i=1; i<=n; i++) {
cin>>c[i];
}
int from,to,wight;
num_edge=0;
for(int i=0; i<m; i++) {
cin>>from>>to>>wight;
addEdge(from,to,wight);
}
num_ans=0;
dijkstra(sp);
// for(int i=1; i<=n; i++) {
// cout<<dis[i]<<" ";
// }
int send=0,end_send=inf;
int back=0,end_back=inf;
int tmp=0,end_n;
int size;
if(num_ans==0) {
cout<<"no way!";
} else {
for(int i=0; i<num_ans; i++) {
size=ans[i].size();
for(int j=size-2; j>=0; j--) {
int index=ans[i][j];
if(c[index]<cmax/2) {
if(tmp>=cmax/2-c[index]) {
tmp-=(cmax/2-c[index]);
} else {
send+=(cmax/2-c[index]-tmp);
tmp=0;
}
} else if(c[index]>cmax/2) {
tmp+=c[index]-cmax/2;
}
}
back=tmp;
if(end_send>=send) {
if(end_send==send) {
if(end_back>back) {
end_n=i;
end_send=send;
end_back=back;
}
} else {
end_n=i;
end_send=send;
end_back=back;
}
}
}
}
cout<<end_send<<" ";
size=ans[end_n].size();
for(int i=size-1; i>=0; i--) {
cout<<ans[end_n][i];
if(i!=0) {
cout<<"->";
}
}
cout<<" "<<end_back;
return 0;
}
3.2 无向图的邻接表表示法的初始化(13/30) #
这个在 1021 Deepest Root遇到过这个问题,就是在初始化的时候,需要把边的两个方向都存储。
for(int i=0; i<m; i++) {
cin>>from>>to>>wight;
addEdge(from,to,wight);
addEdge(to,from,wight);
}
但是当改了这个地方,反而分更低了(13/30)
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=5005;
const int maxm=5010000;
int cmax,n,m,sp,c[maxn];
int head[maxn],vis[maxn],dis[maxn],num_edge;
int pre[maxn];
int num_ans;
vector<int>ans[maxn];
struct Edge {
int to,wight,next;
} edge[maxm];
void addEdge(int from, int to,int wight) { //头插法
edge[++num_edge].to=to;
edge[num_edge].wight=wight;
edge[num_edge].next=head[from];
head[from]=num_edge;
}
struct node { //到固定结点的距离
int dis;
int pos;
bool operator <( const node &x )const {
return x.dis < dis;
}
};
priority_queue<node>q;
void dijkstra(int sp) {
for(int i=1; i<=n; i++) {
dis[i]=inf;
vis[i]=0;
pre[i]=-1;
}
//从0点开始
q.push((node) {
0,0
});
dis[0]=0;
pre[0]=0;
while(!q.empty()) {
node u=q.top();
q.pop();
int pos=u.pos;
if(vis[pos]) {
continue;
}
vis[pos]=1;
for(int i=head[pos]; i; i=edge[i].next) {
int to=edge[i].to;
if(to==sp) { //当结点为sp
if(dis[to]>=dis[pos]+edge[i].wight) { //注意为 >=
if(dis[to]>dis[pos]+edge[i].wight) { //>时,直接更新所有
num_ans=0; //更新所有就是将num_ans重新记录
dis[to]=dis[pos]+edge[i].wight;
pre[to]=pos;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
ans[num_ans].clear();
for(int j=to; j; j=pre[j]) { //保存路径
ans[num_ans].push_back(j);
}
ans[num_ans].push_back(0); //最后的0结点别忘了
num_ans++;
}
} else { //当结点不是sp
if(dis[to]>dis[pos]+edge[i].wight) {
dis[to]=dis[pos]+edge[i].wight;
pre[to]=pos;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
}
}
}
}
int main() {
cin>>cmax>>n>>sp>>m;
for(int i=1; i<=n; i++) {
cin>>c[i];
}
int from,to,wight;
num_edge=0;
for(int i=0; i<m; i++) {
cin>>from>>to>>wight;
addEdge(from,to,wight);
addEdge(to,from,wight);
}
num_ans=0;
dijkstra(sp);
int send=0,end_send=inf;
int back=0,end_back=inf;
int tmp=0,end_n;
int size;
if(num_ans==0) { //
cout<<"no way!";
} else {
for(int i=0; i<num_ans; i++) {
size=ans[i].size();
for(int j=size-2; j>=0; j--) {
int index=ans[i][j];
if(c[index]<cmax/2) {
if(tmp>=cmax/2-c[index]) {
tmp-=(cmax/2-c[index]);
} else {
send+=(cmax/2-c[index]-tmp);
tmp=0;
}
} else if(c[index]>cmax/2) {
tmp+=c[index]-cmax/2;
}
}
back=tmp;
if(end_send>=send) {
if(end_send==send) {
if(end_back>back) {
end_n=i;
end_send=send;
end_back=back;
}
} else {
end_n=i;
end_send=send;
end_back=back;
}
}
}
}
cout<<end_send<<" ";
size=ans[end_n].size();
for(int i=size-1; i>=0; i--) {
cout<<ans[end_n][i];
if(i!=0) {
cout<<"->";
}
}
cout<<" "<<end_back;
return 0;
}
3.3 pre[]
数组的更新(27/30)
#
直到上面的错误的检查以后,我陷入了很长时间的沉思。最后还是用的白盒测试法
,主要就是下面的这些个用例:
3.3.1 用例1 #
输入:
10 3 3 5
7 0 6
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
输出:
0 0->1->3 3
3.3.2 用例2 #
输入:
10 3 3 5
6 0 6
0 1 1
0 2 1
0 3 2
1 3 1
2 3 1
输出:
0 0->3 1
3.3.3 用例3 #
输入:
10 2 2 2
2 10
0 1 1
1 2 1
输出:
3 0->1->2 5
3.3.4 用例4 #
输入:
10 3 3 3
11 0 10
0 1 1
1 2 1
2 3 1
输出:
0 0->1->2->3 6
3.3.5 用例5 #
输入:
10 4 4 5
6 7 5 0
0 1 1
0 2 1
1 3 1
2 3 1
3 4 1
输出:
3 0->2->3->4 0
3.3.6 用例6 #
输入:
10 4 4 4
6 0 11 0
0 1 1
1 2 1
2 3 1
3 4 1
输出:
4 0->1->2->3->4 1
3.3.7 用例7 #
输入:
10 4 4 8
0 11 3 0
0 1 2
0 2 3
0 3 2
1 2 1
2 3 1
1 4 2
2 4 1
3 4 2
输出:
0 0->2->4 1
3.3.8 修改过程(27/30) #
我们知道,在dijkstra
的板子中,基本上所有的 pre
更新是在 dis[to]>dis[pos]+edge[i].wight
的条件下,但是在这道题中,对于$S_p$ 结点,条件应该变成dis[to]>=dis[pos]+edge[i].wight
,并且对 >
和 =
讨论。这时就扯出来 pre[]
的修改时机问题。
void dijkstra(int sp) {
for(int i=1; i<=n; i++) {
dis[i]=inf;
vis[i]=0;
pre[i]=-1;
}
//从0点开始
q.push((node) {
0,0
});
dis[0]=0;
pre[0]=0;
while(!q.empty()) {
node u=q.top();
q.pop();
int pos=u.pos;
if(vis[pos]) {
continue;
}
vis[pos]=1;
for(int i=head[pos]; i; i=edge[i].next) {
int to=edge[i].to;
if(to==sp) { //当结点为sp
if(dis[to]>=dis[pos]+edge[i].wight) { //注意为 >=
if(dis[to]>dis[pos]+edge[i].wight) { //>时,直接更新所有
num_ans=0; //更新所有就是将num_ans重新记录
dis[to]=dis[pos]+edge[i].wight;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
pre[to]=pos; //这句话的位置很重要
ans[num_ans].clear();
for(int j=to; j; j=pre[j]) { //保存路径
ans[num_ans].push_back(j);
}
ans[num_ans].push_back(0); //最后的0结点别忘了
num_ans++;
}
} else { //当结点不是sp
if(dis[to]>dis[pos]+edge[i].wight) {
dis[to]=dis[pos]+edge[i].wight;
pre[to]=pos;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
}
}
}
}
同时,我还发现了一个错误,就是在最后比较 send
、back
等变量时,我没有对所有的变量初始化为0。
send=0,back=0,tmp=0; //初始化很重要
修改了这些以后,27/30,剩下了测试点7。
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int inf=0x3fffffff;
const int maxn=5005;
const int maxm=100010000;
int cmax,n,m,sp,c[maxn];
int head[maxn],vis[maxn],dis[maxn],num_edge;
int pre[maxn];
int num_ans;
vector<int>ans[maxn];
struct Edge {
int to,wight,next;
} edge[maxm];
void addEdge(int from, int to,int wight) { //头插法
edge[++num_edge].to=to;
edge[num_edge].wight=wight;
edge[num_edge].next=head[from];
head[from]=num_edge;
}
struct node { //到固定结点的距离
int dis;
int pos;
bool operator <( const node &x )const {
return x.dis < dis;
}
};
priority_queue<node>q;
void dijkstra(int sp) {
for(int i=1; i<=n; i++) {
dis[i]=inf;
vis[i]=0;
pre[i]=-1;
}
//从0点开始
q.push((node) {
0,0
});
dis[0]=0;
pre[0]=0;
while(!q.empty()) {
node u=q.top();
q.pop();
int pos=u.pos;
if(vis[pos]) {
continue;
}
vis[pos]=1;
for(int i=head[pos]; i; i=edge[i].next) {
int to=edge[i].to;
if(to==sp) { //当结点为sp
if(dis[to]>=dis[pos]+edge[i].wight) { //注意为 >=
if(dis[to]>dis[pos]+edge[i].wight) { //>时,直接更新所有
num_ans=0; //更新所有就是将num_ans重新记录
dis[to]=dis[pos]+edge[i].wight;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
pre[to]=pos; //这句话的位置很重要
ans[num_ans].clear();
for(int j=to; j; j=pre[j]) { //保存路径
ans[num_ans].push_back(j);
}
ans[num_ans].push_back(0); //最后的0结点别忘了
num_ans++;
}
} else { //当结点不是sp
if(dis[to]>dis[pos]+edge[i].wight) {
dis[to]=dis[pos]+edge[i].wight;
pre[to]=pos;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
}
}
}
}
int main() {
cin>>cmax>>n>>sp>>m;
for(int i=1; i<=n; i++) {
cin>>c[i];
}
int from,to,wight;
num_edge=0;
for(int i=0; i<m; i++) {
cin>>from>>to>>wight;
addEdge(from,to,wight);
addEdge(to,from,wight);
}
num_ans=0;
dijkstra(sp);
int send=0,end_send=inf;
int back=0,end_back=inf;
int tmp=0,end_n;
int size;
if(num_ans==0) { //
cout<<"no way!";
} else {
for(int i=0; i<num_ans; i++) {
send=0,back=0,tmp=0; //初始化很重要
size=ans[i].size();
for(int j=size-2; j>=0; j--) {
int index=ans[i][j];
if(c[index]<cmax/2) {
if(tmp>=cmax/2-c[index]) {
tmp-=(cmax/2-c[index]);
} else {
send+=(cmax/2-c[index]-tmp);
tmp=0;
}
} else if(c[index]>cmax/2) {
tmp+=c[index]-cmax/2;
}
}
back=tmp;
if(end_send>send) {
end_n=i;
end_send=send;
end_back=back;
} else if(end_send==send) {
if(end_back>=back) {
end_n=i;
end_send=send;
end_back=back;
}
}
}
}
cout<<end_send<<" ";
size=ans[end_n].size();
for(int i=size-1; i>=0; i--) {
cout<<ans[end_n][i];
if(i!=0) {
cout<<"->";
}
}
cout<<" "<<end_back;
return 0;
}
3.4 测试点 7 #
这道题在牛客上也有,
牛客题目链接。牛客网的所有测试案例可以通过,但是可能会卡在PAT的第7个测试点。因为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.
即相同路径长度排序应该是先比较send较小的情况,其次在比较back较小的情况。
但是我寻思我也考虑到了,但 测试点7
还是卡着。
3.4.1 问题本质 #
后来我终于找到了问题。其实,我们不可以用一维数组 pre[]
直接记录所有路径,因为求解最短路可能会有多个前驱。如果你理解不了,可以看下面的这个用例:
输入:
10 6 6 8
8 9 4 6 7 2
0 1 1
0 2 1
1 3 1
2 3 1
3 4 1
3 5 2
4 6 1
5 6 1
输出:
0 0->1->3->4->6 0
对于上面我的代码,每次都有更新,运行到点3
时, pre[3]
可以为两个值 1
或者 2
,但是由于运行时会更新 pre[]
,而 点1
的路径被覆盖了,所以最后的路径少记录了一条,最后比较的时候当然也会出错。
3.4.2 修改方法 #
将 int pre[maxn];
修改为 vector<int> pre[maxn];
,然后将所有的前驱记录。当然修改这个点之后,就不能用简单的 for
循环遍历了,需要用到 dfs
。 在 dfs
时,保存所有路径,这个路径的保存可以参考二叉树的遍历和路径保存。具体代码如下:
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int inf=0x3fffffff;
const int maxn=505;
const int maxm=100010000;
int cmax,n,m,sp,c[maxn];
int head[maxn],vis[maxn],dis[maxn],num_edge;
vector<int> pre[maxn];
int num_ans;
vector<int>ans[maxn];
struct Edge {
int to,wight,next;
} edge[maxm];
void addEdge(int from, int to,int wight) { //头插法
edge[++num_edge].to=to;
edge[num_edge].wight=wight;
edge[num_edge].next=head[from];
head[from]=num_edge;
}
struct node { //到固定结点的距离
int dis;
int pos;
bool operator <( const node &x )const {
return x.dis < dis;
}
};
priority_queue<node>q;
void dijkstra(int sp) {
for(int i=1; i<=n; i++) {
dis[i]=inf;
vis[i]=0;
pre[i].clear();
}
//从0点开始
q.push((node) {
0,0
});
dis[0]=0;
pre[0].push_back(0);
while(!q.empty()) {
node u=q.top();
q.pop();
int pos=u.pos;
if(vis[pos]) {
continue;
}
vis[pos]=1;
for(int i=head[pos]; i; i=edge[i].next) {
int to=edge[i].to;
if(dis[to]>=dis[pos]+edge[i].wight) { //注意为 >=
if(dis[to]>dis[pos]+edge[i].wight) { //>时,直接更新所有
pre[to].clear();
dis[to]=dis[pos]+edge[i].wight;
if(!vis[to]) {
q.push((node) {
dis[to],to
});
}
}
pre[to].push_back(pos);
}
}
}
}
void dfs(int v,vector<int> a) {
a.push_back(v);
if(v==0) {
ans[num_ans++]=a;
a.clear();
return;
}
for(int i=0; i<pre[v].size(); i++) {
dfs(pre[v][i],a);
}
}
int main() {
cin>>cmax>>n>>sp>>m;
for(int i=1; i<=n; i++) {
cin>>c[i];
}
int from,to,wight;
num_edge=0;
for(int i=0; i<m; i++) {
cin>>from>>to>>wight;
addEdge(from,to,wight);
addEdge(to,from,wight);
}
num_ans=0;
dijkstra(sp);
vector<int> a;
dfs(sp,a);
int send=0,end_send=inf;
int back=0,end_back=inf;
int tmp=0,end_n;
int size;
if(num_ans==0) { //
cout<<"no way!";
} else {
for(int i=0; i<num_ans; i++) {
send=0,back=0,tmp=0; //初始化很重要
size=ans[i].size();
for(int j=size-2; j>=0; j--) {
int index=ans[i][j];
if(c[index]<cmax/2) {
if(tmp>=cmax/2-c[index]) {
tmp-=(cmax/2-c[index]);
} else {
send+=(cmax/2-c[index]-tmp);
tmp=0;
}
} else if(c[index]>cmax/2) {
tmp+=c[index]-cmax/2;
}
}
back=tmp;
// cout<<send<<" ";
// size=ans[i].size();
// for(int j=size-1; j>=0; j--) {
// cout<<ans[i][j];
// if(j!=0) {
// cout<<"->";
// }
// }
// cout<<" "<<back<<endl;
if(end_send>=send) {
if(end_send==send) {
if(end_back>=back) {
end_n=i;
end_send=send;
end_back=back;
}
} else if(end_send>send) {
end_n=i;
end_send=send;
end_back=back;
}
}
}
}
cout<<end_send<<" ";
size=ans[end_n].size();
for(int i=size-1; i>=0; i--) {
cout<<ans[end_n][i];
if(i!=0) {
cout<<"->";
}
}
cout<<" "<<end_back;
return 0;
}