1018 Public Bike Management/公共自行车管理
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 S3, we have 2 different shortest paths:
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.
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
思路
题意:本道题满足每个站点自己调整,调整的最优的数量是等于 C m a x C_{max} Cmax 的一半,要选择最短路,且最短路 P B M C PBMC PBMC 提供的车的数量最少且退回车的数量最少的方案输出。
- 最短路:我们这边可以预处理一下dist,求出最短路——
dijistra
算法。 - 方案:本代码采用
dfs
,倒序遍历,如果从 s p s_p sp 搜到了 P B M C PBMC PBMC 的时候,我们就开始处理方案。如何处理呢? - 我们首先可以预处理出每个站点的容量里理想状态的差值,有正有负,如果是正,说明盈余,如果负,说明剩余。
- 然后我们对该路径上所有的点出源点外加上刚刚提到的值,如果这个值是大于 0 0 0 的话,说明盈余,此时要退回。
- 如果这个值在计算的过程中出现了负数且最终算出来的还是负数,说明不够,此时就得从 P B M C PBMC PBMC 拿出来。
- 如果这个值在计算的过程中出现了负数且最终算出来的是正数,这里就要注意了:因为调整只能从上到下调整,不能从下到上调整
就比如测试1:
10 4 4 4
2 9 9 9
0 1 1
1 2 1
2 3 1
3 4 1
输出
3 0->1->2->3->4 12
测试2:
10 4 4 4
5 9 2 9
0 1 1
1 2 1
2 3 1
3 4 1
输出
0 0->1->2->3->4 5
因此就以上三种情况,我们只需要简单的分类讨论即可。
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#define x first
#define y second
using namespace std;
const int N = 510,M = 4010;
typedef pair<int,int>PII;
int e[M],ne[M],f[M],h[N],idx;
bool st[N];
int dist[N];
int n,m,maxv,sp;
int w[M];
vector<int>path;
vector<int> ans;
int back;
int res=-0x3f3f3f3f;
void add(int a,int b,int c){
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void dijistra(){
priority_queue<PII,vector<PII>,greater<PII>>q;
memset(dist,0x3f,sizeof dist);
dist[0]=0;
q.push({dist[0],0});
while(q.size()){
int ver=q.top().y;
q.pop();
if(st[ver])continue;
st[ver]=true;
for(int i=h[ver];~i;i=ne[i]){
int j=e[i];
if(dist[j]>dist[ver]+f[i]){
dist[j]=dist[ver]+f[i];
q.push({dist[j],j});
}
}
}
}
void dfs(int u){
if(!u){
int now=0,minv=0;//now就是当前路径上需要贡献的车辆
for(int i=path.size()-2;i>=0;i--){//此时是包含最后一个点的
now+=w[path[i]];
minv=min(minv,now);
}
if(minv>res){
res=minv;
back=now-minv;
ans.clear();
for(int i=path.size()-1;i>=0;i--){
ans.push_back(path[i]);
}
}
// cout<<now<< ' '<<minv<<endl;
if(minv==res){
if(now-minv<back){
back=now-minv;
ans.clear();
for(int i=path.size()-1;i>=0;i--){
ans.push_back(path[i]);
}
}
}
return;
}
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(dist[j]==dist[u]-f[i]){
path.push_back(j);
dfs(j);
path.pop_back();
}
}
}
int main(){
cin>>maxv>>n>>sp>>m;
memset(h,-1,sizeof h);
for(int i=1;i<=n;i++){
cin>>w[i];
w[i]-=maxv/2;
}
while(m--){
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
dijistra();
path.push_back(sp);
dfs(sp);//倒着做
cout<<-res<<' ';
cout<<ans[0];
for(int i=1;i<ans.size();i++){
cout<<"->"<<ans[i];
}
cout<<' '<<back;
return 0;
}