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
思路
这道题有点难哈,一个月前也是困扰了我很久很久,那个时候的我甚至不知道题目在说啥……
那么首先来说一下这到题目的意思,就算不看文字只看图片也能猜到这是一道图论的题目叭,然后题目的具体意思就是让你求从PBMC(也就是起点,这题的起点固定了)到目标点Sp的最短路。
但是这还不够,因为题目要求在沿途的路中,我们要把所有经过的站点全部调整为所谓“完美状态”(每个站点的点权是最大容量的一半),因此,在最短的几条路里面呢,我们要选择从PBMC发出最少(自行车)的那条路,如果碰巧那几条最短路中,从PBMC发出的(自行车)数量相同,那么我们还要选择(自行车)带回PBMC最少的那条路。
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.
所以我们这题有3个标尺:①优先距离最短;②次级优先PBMC发出最少;③最次级优先PBMC带回最少。
那么很显然一遍Dijkstra是无法完成的(在寻找最短路的路途中,根本无法处理从PBMC发出还是带回的数量,以及当前站点是有余量还是要补给),因此,需要用Dijkstra+DFS的方法去完成(终于回到了最初的起点……【《算法笔记》学习日记——C/C++快速入门(上)】开篇就提到过这题,我就是因为太难了下不了手,就选择从头到尾学《算法笔记》这本书……现在看来还有点效果)。
那么具体怎么做呢?如果用Dijkstra+DFS的话,Dijkstra还是比较简单的,直接找最短路,也不用考虑什么点权的问题,碰到优化更新d[v]的时候,记得把v的前驱u记录好就行(pre[v].push_back(u))。
然后我们用Dijkstra找完最短路了(可能不止一条,但是没关系,因为我们是用vector容器记录的前驱,因此完全可以回溯出所有的最短路),就用DFS的方法去搜索最短路。
DFS的部分呢主要分为两个部分:
①如果当前点v还不是起点,那很简单,先把当前点v压入暂时路径tempPath里(这里之所以用tempPath是因为题目要输出的是最终选择的路径,这里只是存放搜到的一条路),然后继续往前搜,找到v的所有前驱,一个一个DFS过去(“岔路口”),当从那几个“岔路口”回来时,把那个点pop_back()掉;
②如果当前点v是起点,那么就先把当前点压入tempPath里。好,到现在为止,其中的一条最短路已经被我们搜到了,它就放在tempPath里,这不过是倒序的(因为我们是从终点不断找前驱的,所以是倒着的),因此,我们需要从倒数第二个编号开始处理(因为倒数第一个是PBMC,是起点,不需要处理,默认存在无限多的自行车)。在这之前,我们需要用need和remain来实时计算从PBMC带出去和带回来的自行车数量。比如,以题图的例子来说,当到达S1时,因为多余了1辆自行车,因此need = 0,而remain = 1,到S3之后呢,它总共需要5辆,那我先把剩余的自行车给它remain = 0,然后呢还要4辆,因此need = 4,这样,一种情况就处理好了。
为了能够更新PBMC发出最少以及PBMC带回最少,我们还需要设一个全局变量minNeed和minRemain,这样的话,每处理完一条路之后,对need和minNeed作比较,如果need更小,说明是发出最少的,那么这种情况是我们想要的,于是更新所有数值(minNeed = need,minRemain = remain,path = tempPath),如果need和minNeed相等,就选择第三标尺:对remain和minRemain作比较,如果remain更小,说明是带回最少的,是我们想要的,于是更新所有数值。
最后minNeed和minRemain就是我们要输出的两头的数值,path就是我们存放的最终选择的那条路。
【思路参考了晴神的思路哈,真的写的很清楚,而且一读就懂】
代码
#include<cstdio>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 501;
const int INF = 123123123;
struct node{
int v;//目标点
int dis;//边权
};
vector<node> G[maxn];
vector<int> pre[maxn];
vector<int> path, tempPath;
int weight[maxn] = {0};//点权
int d[maxn] = {0};
bool vis[maxn] = {false};
int minNeed = INF, minRemain = INF;
//minNeed是从PBMC发出的最少自行车数,minRemain是结束之后需要带回PBMC的最少自行车数
void Dijkstra(int n, int s){//n是顶点个数,s是起点
fill(d, d+maxn, INF);
d[s] = 0;
for(int i=0;i<n;i++){
int u = -1;
int MIN = INF;
for(int j=0;j<n;j++){
if(vis[j]==false&&d[j]<MIN){
u = j;
MIN = d[j];
}
}
if(u==-1) return;
vis[u] = true;
for(int j=0;j<G[u].size();j++){
int v = G[u][j].v;
if(vis[v]==false){
if(d[u]+G[u][j].dis<d[v]){
d[v] = d[u]+G[u][j].dis;
pre[v].clear();
pre[v].push_back(u);//v的前驱是u
}
else if(d[u]+G[u][j].dis==d[v]){
pre[v].push_back(u);
}
}
}
}
}
void dfs(int v, int s){
if(v==s){
tempPath.push_back(v);
int need, remain;
need = remain = 0;//到达每个站点时,当前站点所需要(自行车)的数量(need),和多余的(自行车)数量(remain)
for(int i=tempPath.size()-2;i>=0;i--){//从起点的后一个点开始处理(起点是PBMC)
int id = tempPath[i];
if(weight[id]>0) remain += weight[id];//这里要是+=,=号会覆盖
else if(weight[id]==0) continue;//已经是完美状态了,往下一个点
else{//如果是负的,说明要补充
if(remain>=abs(weight[id])){//如果存量足够补充这个站点的自行车
remain -= abs(weight[id]);
}
else{//如果存量不够
need += abs(weight[id])-remain;//还需要从PBMC带出这么多来补充
//这里也要写+=,不然会覆盖
remain = 0;
}
}
}
//当一条最短路处理完之后:
if(need<minNeed){//如果当前最短路更优
minNeed = need;//更新相关值
minRemain = remain;
path = tempPath;//更新最短路
}
else if(need==minNeed){//如果从PBMC带出去的一样少
if(remain<minRemain){//此时要选择带回去最少的那种方案
minNeed = need;
minRemain = remain;
path = tempPath;//更新最短路
}
}
return;
}
tempPath.push_back(v);
for(int i=0;i<pre[v].size();i++){
dfs(pre[v][i], s);
tempPath.pop_back();
}
}
int main(){
//freopen("input.txt", "r", stdin);//用来测试数据的,请无视
int Cmax, N, Sp, M;
//Cmax是每个站自行车的最大容量
//N是顶点数
//Sp是终点
//M是边数
scanf("%d%d%d%d", &Cmax, &N, &Sp, &M);
for(int i=1;i<=N;i++){
int tmpweight;
scanf("%d", &tmpweight);
weight[i] = tmpweight-Cmax/2;//减去完美状态
//这样一来,如果weight[i]是正的,说明冗余,需要带走自行车
//如果weight[i]是负的,说明不够,需要补充自行车
}
weight[0] = INF;//PBMC的点权为无穷大
for(int i=0;i<M;i++){
node tmp;
int a, b, tmpdis;
scanf("%d%d%d", &a, &b, &tmpdis);
tmp.dis = tmpdis;
tmp.v = a;
G[b].push_back(tmp);
tmp.v = b;
G[a].push_back(tmp);
}
Dijkstra(N, 0);
dfs(Sp, 0);
printf("%d ", minNeed);//需要从PBMC带出去的
for(int i=path.size()-1;i>=0;i--){
if(i==path.size()-1) printf("%d", path[i]);
else printf("->%d", path[i]);
}
printf(" %d", minRemain);//需要带回去的
return 0;
}