Boostable

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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 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 1
Sample Output:
3 0->2->3 0

又是属于比较烦的题目,直接dijkstra。但在比较路径大小的时候需要考虑:1)先比较到达时间,若时间小则路径小;2)若时间一样,则比较send的自行车数量,数量小的路径小;3)若send
的自行车数量也一样,则比较back的自行车数量,back的数量小则路径小。另外也要注意send与back的更新,后面station多出来的自行车是无法补给前面station的。
代码
  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 #define MAXV  501
  5 
  6 int map[MAXV][MAXV];
  7 int reminder[MAXV];
  8 int send[MAXV];
  9 int back[MAXV];
 10 int times[MAXV];
 11 int flag[MAXV];
 12 int path[MAXV][MAXV];
 13 
 14 int findMinTimesPoint(int);
 15 int main()
 16 {
 17     int Cmax,N,Sp,M,s,e,i;
 18     while(scanf("%d%d%d%d",&Cmax,&N,&Sp,&M) != EOF){
 19         for(i=1;i<=N;++i)
 20             scanf("%d",&reminder[i]);
 21         memset(map,0,sizeof(map));
 22         for(i=0;i<M;++i){
 23             scanf("%d%d",&s,&e);
 24             scanf("%d",&map[s][e]);
 25             map[e][s] = map[s][e];
 26         }
 27         memset(send,0,sizeof(send));
 28         memset(back,0,sizeof(back));
 29         memset(flag,0,sizeof(flag));
 30         memset(path,0,sizeof(path));
 31         s = 0;
 32         send[s] = 0;
 33         back[s] = 0;
 34         flag[s] = 1;
 35         path[0][0] = 1;
 36         path[0][1] = 0;
 37         for(i=1;i<=N;++i){
 38             times[i] = -1;
 39             if(map[s][i]){
 40                 times[i] = map[s][i];
 41                 send[i] = Cmax / 2 - reminder[i];
 42                 if(send[i] < 0)
 43                     send[i] = 0;
 44                 back[i] = reminder[i] - Cmax / 2;
 45                 if(back[i] < 0)
 46                     back[i] = 0;
 47                 path[i][0] = 2;
 48                 path[i][1] = 0;
 49                 path[i][2] = i;
 50             }
 51         }
 52         times[s] = 0;
 53         while(!flag[Sp]){
 54             s = findMinTimesPoint(N);
 55             flag[s] = 1;
 56             for(i=1;i<=N;++i){
 57                 if(!flag[i] && map[s][i]){
 58                     if(times[i] == -1 || (times[i] > times[s] + map[s][i])){
 59                         times[i] = times[s] + map[s][i];
 60                         path[i][0] = path[s][0] + 1;
 61                         int j;
 62                         for(j=1;j<=path[s][0];++j)
 63                             path[i][j] = path[s][j];
 64                         path[i][j] = i;
 65                         int t = Cmax / 2 - reminder[i];
 66                         if(t == 0){
 67                             send[i] = send[s];
 68                             back[i] = back[s];
 69                         }
 70                         else if(t > 0){
 71                             if(t-back[s] >= 0){
 72                                 back[i] = 0;
 73                                 send[i] = send[s] + t - back[s];
 74                             }
 75                             else{
 76                                 send[i] = send[s];
 77                                 back[i] = back[s] - t;
 78                             }
 79                         }
 80                         else{
 81                             send[i] = send[s];
 82                             back[i] = back[s] - t;
 83                         } 
 84                     }
 85                     else if(times[i] == times[s] + map[s][i]){
 86                         int t = Cmax / 2 - reminder[i];
 87                         int tSend,tBack;
 88                         if(t==0){
 89                             tSend = send[s];
 90                             tBack = back[s];
 91                         }
 92                         else if(t > 0){
 93                             if(t-back[s] >= 0){
 94                                 tBack = 0;
 95                                 tSend = send[s] + t - back[s];
 96                             }
 97                             else{
 98                                 tSend = send[s];
 99                                 tBack = back[s] - t;
100                             }
101                         }
102                         else{
103                             tSend = send[s];
104                             tBack = back[s] - t;
105                         }
106                         if(tSend < send[i] || (tSend == send[i] && tBack < back[i])){
107                             send[i] = tSend;
108                             back[i] = tBack;
109                             path[i][0] = path[s][0] + 1;
110                             int j;
111                             for(j=1;j<=path[s][0];++j)
112                                 path[i][j] = path[s][j];
113                             path[i][j] = i;
114                         }
115                     }
116                 }
117             }
118         }
119         printf("%d %d",send[Sp],path[Sp][1]);
120         for(i = 2;i<=path[Sp][0];++i){
121             printf("->%d",path[Sp][i]);
122         }
123         printf(" %d\n",back[Sp]);
124     }
125     return 0;
126 }
127 
128 int findMinTimesPoint(int n)
129 {
130     int i = 1;
131     int minPointInd;
132     while(i<=n && (times[i] == -1 || flag[i]))
133         ++i;
134     if(i > n)
135         return -1;
136     minPointInd = i;
137     for(;i<=n;++i){
138         if(!flag[i] && times[i] != -1 && (times[i] < times[minPointInd] || 
139             (times[i] == times[minPointInd] && send[i]<send[minPointInd]) ||
140             (send[i]==send[minPointInd] && back[i]<back[minPointInd])))
141             minPointInd = i;
142     }
143     return minPointInd;
144 }

 



posted on 2014-02-24 19:54  Boostable  阅读(481)  评论(0编辑  收藏  举报