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 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 Mlines 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.
思路:
Dijkstra求出所有最短路 + DFS求出min need的路
在DFS中回溯的時候,註意:從起點(PBMC)開始走,先前的back可以抵消後面的need,但是後面的back不能抵消先前的need
前面的need也不影響後面的back;
也就是說,每個點的不足,由PBMC或者其先前的站點補足,與下遊站點無關
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 using namespace std; 5 const int inf = 999999; 6 const int maxn = 550; 7 8 int cmax,n,sp,m; 9 int dismap[maxn][maxn]; 10 int w[maxn],dis[maxn]; 11 vector<int> pre[maxn]; 12 vector<int> path,tmppath; 13 int minneed=inf ,minback=inf; 14 15 void dijkstra(int s){ 16 int vis[maxn]; fill(vis,vis+maxn,0); 17 fill(dis,dis+maxn,inf); 18 dis[s]=0; 19 for(int i=0;i<=n;i++){ 20 int minv,mind=inf; 21 for(int v=0;v<=n;v++){ 22 if(!vis[v]&&dis[v]<mind){ 23 mind=dis[v]; 24 minv=v; 25 } 26 } 27 if(mind==inf) break; 28 vis[minv]=1; 29 for(int v=0;v<=n;v++){ 30 if(!vis[v]&&dismap[minv][v]!=inf){ 31 if(dis[v]>mind+dismap[minv][v]){ 32 dis[v]=mind+dismap[minv][v]; 33 //printf("# %d %d %d\n",dis[v],minv,v); 34 pre[v].clear(); 35 pre[v].push_back(minv); 36 }else if(dis[v]==mind+dismap[minv][v]){ 37 pre[v].push_back(minv); 38 } 39 } 40 } 41 } 42 } 43 44 void dfs(int v){ 45 tmppath.push_back(v); 46 if(v==0){ 47 int back=0,need=0; 48 for(int i=tmppath.size()-2;i>=0;i--){ 49 int id=tmppath[i]; 50 51 if(w[id]>=0){ 52 back+=w[id]; 53 54 }else{ 55 if(-w[id]<back){ 56 back+=w[id]; 57 }else{ 58 need+=(-w[id])-back; 59 back=0; 60 //printf("# %d %d %d\n",id,w[id],need); 61 } 62 } 63 } 64 if(need<minneed){ 65 path=tmppath; 66 minneed=need; 67 minback=back; 68 }else if(need==minneed&&back<minback){ 69 path=tmppath; 70 minback=back; 71 } 72 tmppath.pop_back(); 73 return; 74 } 75 for(int i=0;i<pre[v].size();i++){ 76 dfs(pre[v][i]); 77 } 78 tmppath.pop_back(); 79 } 80 81 int main(){ 82 fill(dismap[0],dismap[0]+maxn*maxn,inf); 83 84 scanf("%d %d %d %d",&cmax,&n,&sp,&m); 85 for(int i=1;i<=n;i++){ 86 scanf("%d",&w[i]); 87 w[i]-=cmax/2; 88 } 89 int a,b,tmpd; 90 for(int i=0;i<m;i++){ 91 scanf("%d %d %d",&a,&b,&tmpd); 92 dismap[a][b]=dismap[b][a]=tmpd; 93 } 94 //printf("# %d\n",w[3]); 95 dijkstra(0); 96 dfs(sp); 97 printf("%d ",minneed); 98 for(int i=path.size()-1;i>=0;i--){ 99 printf("%d",path[i]); 100 if(i!=0) printf("->"); 101 } 102 printf(" %d",minback); 103 }
1018 Public Bike Management