1018. Public Bike Management (30)
题目连接:https://www.patest.cn/contests/pat-a-practise/1018
原题如下:
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 S3, we have 2 different shortest paths:
1. 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.
2. 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 1Sample Output:
3 0->2->3 0
这道题和之前的那道Emergency很想,我一开始就是按照那道题的思路来的,DFS结合Dijkra,但后来发现有一个难点就是路径的记录问题,我自己的方法
不太对,参考了他人的解法,用vector真的很方便啊……
还有一点容易出错的地方就是当发放自行车是,发放的数量是累加的,只要发放(即当回收的不够发放时),则回收数即为0;回收数是根据发放和回收变化的
(说的有点乱,具体看代码吧)
1 #include<iostream> 2 #include<stdio.h> 3 #include<vector> 4 #include<functional> 5 #define MAXN 505 6 #define INFINITY 65535 7 using namespace std; 8 9 int Cmax,Sp,M,N; 10 int SBike[MAXN],G[MAXN][MAXN],visited[MAXN]; 11 int dist[MAXN]; 12 vector<int>result; 13 vector<int>CurResult; 14 int SendBike=0,GetBike=0; 15 int MinSendBike=INFINITY,MinGetBike=INFINITY; 16 17 int FindMin() 18 { 19 int tmp=INFINITY; 20 int Mini=-1,i; 21 for (i=1;i<=N;i++) 22 { 23 if (!visited[i] && dist[i]<tmp) 24 { 25 tmp=dist[i]; 26 Mini=i; 27 } 28 } 29 return Mini; 30 } 31 32 void Dijkra() 33 { 34 int i,v; 35 dist[0]=0; 36 visited[0]=1; 37 for (i=1;i<=N;i++) 38 { 39 if (G[0][i]<INFINITY)dist[i]=G[0][i]; 40 } 41 42 while (1) 43 { 44 v=FindMin(); 45 if (v==-1)break; 46 visited[v]=1; 47 for (i=1;i<=N;i++) 48 { 49 if (!visited[i] && dist[i]>dist[v]+G[v][i]) 50 dist[i]=dist[v]+G[v][i]; 51 } 52 } 53 } 54 55 void Count() 56 { 57 int i; 58 GetBike=0; 59 SendBike=0; 60 for (i=0;i<CurResult.size();i++) 61 { 62 if (GetBike+SBike[CurResult[i]]>=Cmax/2)GetBike=GetBike+SBike[CurResult[i]]-Cmax/2; 63 else {SendBike+=(Cmax/2-GetBike-SBike[CurResult[i]]);GetBike=0;} 64 } 65 } 66 67 void DFS(int src) 68 { 69 if (src==Sp) 70 { 71 Count(); 72 if ((SendBike<MinSendBike)||(SendBike==MinSendBike && GetBike<MinGetBike)) 73 { 74 MinSendBike=SendBike; 75 MinGetBike=GetBike; 76 result=CurResult; 77 } 78 return ; 79 } 80 int i; 81 for (i=1;i<=N;i++) 82 { 83 if (src!=i && !visited[i] && dist[i]==dist[src]+G[src][i]) 84 { 85 CurResult.push_back(i); 86 visited[i]=1; 87 DFS(i); 88 visited[i]=0; 89 CurResult.pop_back(); 90 } 91 } 92 } 93 int main() 94 { 95 scanf("%d %d %d %d",&Cmax,&N,&Sp,&M); 96 int i,j,a,b,v; 97 for (i=1;i<=N;i++)scanf("%d",&SBike[i]); 98 99 for (i=0;i<=N;i++) 100 { 101 visited[i]=0; 102 dist[i]=INFINITY; 103 for (j=0;j<=N;j++)G[i][j]=INFINITY; 104 } 105 106 for (i=0;i<M;i++) 107 { 108 scanf("%d %d %d",&a,&b,&v); 109 G[a][b]=G[b][a]=v; 110 } 111 112 Dijkra(); 113 for (i=0;i<=N;i++)visited[i]=0; 114 visited[0]=1; 115 116 DFS(0); 117 118 printf("%d 0",MinSendBike); 119 for (i=0;i<result.size();i++)printf("->%d",result[i]); 120 printf(" %d",MinGetBike); 121 return 0; 122 }