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.
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:
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.
题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024
1 | Sample Input: |
思路:
dijskstra+dfs1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#include<iostream>
#include<vector>
#include<cmath>
#define INF 0x3f3f3f
#define MAX 510
using namespace std;
int cmax,n,sp,m;
int dis[MAX],g[MAX][MAX],wei[MAX];
vector<int> pre[MAX],tmp,path;
int vis[MAX];
int mini=INF,lremain=INF;
#define INF 0x3f3f3f
void dij(int src){
fill(dis,dis+MAX,INF);
dis[src]=0;
for(int i=0;i<=n;i++){
int min=INF,u=-1;
for(int j=0;j<=n;j++){
if(!vis[j]&&dis[j]<min){
min=dis[j];u=j;
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<=n;v++){
if(vis[v]||g[u][v]==INF) continue;
if(dis[u]+g[u][v]<dis[v]){
dis[v]=dis[u]+g[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if(dis[u]+g[u][v]==dis[v]){
pre[v].push_back(u);
}
}
}
}
int remain=0,need=0;
void dfs(int u){
if(u==0){
tmp.push_back(u);
for(int i=tmp.size()-1;i>=0;i--){
int v=tmp[i];
if(wei[v]>0){
remain+=wei[v];
}
else{
if(abs(wei[v])>remain){
need+=abs(wei[v])-remain;
remain=0;
}
else {
remain-=abs(wei[v]);
}
}
}
if(need<mini){
mini=need;
path=tmp;
lremain=remain;
}
else if(need==mini&&remain<lremain){
lremain=remain;
path=tmp;
}
tmp.pop_back();
remain=0,need=0;
return;
}
tmp.push_back(u);
for(int k=0;k<pre[u].size();k++)
dfs(pre[u][k]);
tmp.pop_back();
}
int main(){
// freopen("20.txt","r",stdin);
fill(g[0],g[0]+MAX*MAX,INF);
cin>>cmax>>n>>sp>>m;
for(int i=1;i<=n;i++){
cin>>wei[i];
wei[i]-=cmax/2;
}
for(int j=0;j<m;j++)
{
int u,v;
cin>>u>>v;
cin>>g[u][v];
g[v][u]=g[u][v];
}
dij(0);
dfs(sp);
cout<<mini<<" ";
for(int i=path.size()-1;i>=0;i--){
cout<<path[i];
if(i!=0) cout<<"->";
}
cout<<" "<<lremain<<endl;
}