1018 Public Bike Management (30)
Posted mr-stn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1018 Public Bike Management (30)相关的知识,希望对你有一定的参考价值。
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.
Figure 1
Figure 1 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:
1. 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.
2. 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 M lines 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.
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 #include<iostream> 2 #include<vector> 3 #include<math.h> 4 using namespace std; 5 const int inf=99999999; 6 vector<int> pre[505], vis(505, false), path, temp; 7 int e[505][505], dis[505], weight[505]; 8 int mintime=inf, minbike=inf, cmax; 9 void dfs(int v){ 10 temp.push_back(v); 11 if(v==0){ 12 int tempmintime=0, tempminbike=0; 13 for(int j=temp.size()-1; j>0; j--){ 14 int id=temp[j], nextid=temp[j-1]; 15 tempmintime += e[id][nextid]; 16 tempminbike += (cmax/2 - weight[nextid]); 17 } 18 if(tempmintime<mintime){ 19 path = temp; 20 mintime = tempmintime; 21 minbike = tempminbike; 22 }else if(tempmintime==mintime && abs(tempminbike)<abs(minbike)){ 23 path = temp; 24 minbike = tempminbike; 25 } 26 temp.pop_back(); 27 return; 28 } 29 for(int i=0; i<pre[v].size(); i++) dfs(pre[v][i]); 30 temp.pop_back(); 31 } 32 33 int main(){ 34 fill(e[0], e[0]+505*505, inf); 35 fill(dis, dis+505, inf); 36 int n, sp, m, i; 37 cin>>cmax>>n>>sp>>m; 38 weight[0]=5; 39 for(i=1; i<=n; i++) scanf("%d", &weight[i]); 40 for(i=0; i<m; i++){ 41 int a, b, c; 42 scanf("%d %d %d", &a, &b, &c); 43 e[a][b]=e[b][a]=c; 44 } 45 pre[0].push_back(0); 46 dis[0]=0; 47 for(i=0; i<=n; i++){ 48 int u=-1, minn=inf; 49 for(int j=0; j<=n; j++){ 50 if(!vis[j] && dis[j]<minn){ 51 minn=dis[j]; 52 u=j; 53 } 54 } 55 if(u==-1) break; 56 vis[u]=true; 57 for(int v=0; v<=n; v++){ 58 if(!vis[v] && e[u][v]!=inf){ 59 if(dis[v]>dis[u]+e[u][v]){ 60 dis[v]=dis[u]+e[u][v]; 61 pre[v].clear(); 62 pre[v].push_back(u); 63 }else if(dis[v]==dis[u]+e[u][v]){ 64 pre[v].push_back(u); 65 } 66 } 67 } 68 } 69 dfs(sp); 70 if(minbike>0) cout<<minbike<<" "; 71 else cout<<0<<" "; 72 for(i=path.size()-1; i>=0; i--){ 73 if(i==path.size()-1) printf("%d", path[i]); 74 else printf("->%d", path[i]); 75 } 76 if(minbike>0) cout<<" "<<0<<endl; 77 else cout<<" "<<-minbike<<endl; 78 return 0; 79 }
以上是关于1018 Public Bike Management (30)的主要内容,如果未能解决你的问题,请参考以下文章
1018. Public Bike Management (30)
1018 Public Bike Management (30 分)
1018 Public Bike Management (30分)