敝人做的第一道POJ题,丫的没想到是如此麻烦的模拟题,做了好久。最后反复TLE,自己各类数据没有问题,源测试数据没下到。
最后找牛人帮调试,终于AC。谢之。
问题出在输入上,测试数据并不按顾客优先级输入,之前对题意理解有偏差。
这道题目有只很牛逼的电梯。在5的整数倍秒打开进人,且有相位鞋的功效,不计碰撞,真tm牛逼!
我的低级代码:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
#include
<
iostream
>
2 #include < string >
3 #include < iomanip >
4 #include < cmath >
5 #include < algorithm >
6 #include < deque >
7 #include < vector >
8 #include < time.h >
9 using namespace std;
10
11 enum preState
12 {
13 s_Outside,
14 s_InElevator,
15 s_InRoom,
16 s_InEleQueue,
17 s_InRoomQueue,
18 s_PlanFinished,
19 };
20 struct timmme
21 {
22 friend istream & operator >> (istream & ,timmme & );
23 int hour;
24 int minute;
25 int second;
26 timmme():hour( 0 ),minute( 0 ),second( 0 ){}
27 timmme( int hr, int mt, int sd):hour(hr),minute(mt),second(sd) {}
28 void print()
29 {
30 printf( " %02d:%02d:%02d " , hour, minute, second);
31 }
32 timmme operator + ( int stay)
33 {
34 timmme tm;
35 int tmp,tmp1;
36 tmp = (second + stay) / 60 ;
37 tm.second = (second + stay) % 60 ;
38 tmp1 = (minute + tmp) / 60 ;
39 tm.minute = (minute + tmp) % 60 ;
40 tm.hour = (hour + tmp1) % 24 ;
41 return tm;
42 }
43 bool operator < (timmme & rhs)
44 {
45 if (hour < rhs.hour)
46 return true ;
47 else if (hour > rhs.hour)
48 return false ;
49 else if (minute < rhs.minute)
50 return true ;
51 else if (minute > rhs.minute)
52 return false ;
53 else if (second < rhs.second)
54 return true ;
55 else return false ;
56 }
57 timmme & operator = ( const timmme & rhs)
58 {
59 second = rhs.second;
60 minute = rhs.minute;
61 hour = rhs.hour;
62 return * this ;
63 }
64 };
65 struct task
66 {
67 friend istream & operator >> (istream & , task & );
68 string xxyy;
69 int stay;
70 bool over;
71 int getRoomNum() // 获得房间号(1-100)
72 {
73 int a = (xxyy[ 2 ] == ' 1 ' ) ? 10 : int (xxyy[ 3 ] - ' 0 ' );
74 return (getFloorNum() - 1 ) * 10 + a;
75 }
76 int getFloorNum() // 获得楼层号(1-10)
77 {
78 return (xxyy[ 0 ] == ' 1 ' ) ? 10 : int (xxyy[ 1 ] - ' 0 ' );
79 }
80 task( string xy, int st):xxyy(xy),stay(st), over( false ){}
81 task():xxyy( " 0101 " ),stay( 0 ), over( false ){}
82 };
83 struct state
84 {
85 timmme start;
86 timmme finish;
87 bool waiting;
88 string description;
89 state():waiting( false ){}
90 void print()
91 {
92 start.print();
93 printf( " " );
94 finish.print();
95 printf( " %s\n " , description.c_str());
96 }
97 };
98 class agent;
99 struct room
100 {
101 timmme freeUntil;
102 deque < agent *> roomQueue;
103 };
104 struct elevator
105 {
106 timmme freeUntil;
107 deque < agent *> eleQueue;
108 };
109 class agent
110 {
111 friend istream & operator >> (istream & , agent & );
112 public :
113 state st;
114 deque < task > plan;
115 agent():prSt(s_Outside){}
116 void SetCurrentTask() { if (plan.size()) currentTask = plan.front(); }
117 task & updateTask();
118 char & getPriority() { return priority; }
119 timmme & getBegintime() { return beginTime; }
120 preState & getPreState() { return prSt; }
121 state getState(vector < room >& , vector < elevator >& );
122 void visit(state & );
123 void print();
124 bool operator < (agent & rhs)
125 {
126 return priority < rhs.getPriority();
127 }
128 private :
129 char priority;
130 timmme beginTime;
131 task currentTask;
132 task preTask;
133 preState prSt;
134 vector < state > route;
135 };
136 task & agent::updateTask() // 更新当前任务
137 {
138 plan.pop_front();
139 return plan.front();
140 }
141
142 state agent::getState(vector < room >& rom, vector < elevator >& elevt)
143 {
144 if ( ! st.waiting) // 仅不处于等待状态的agent才更新state起始时间
145 st.start = beginTime;
146 if (currentTask.over && plan.size() == 1 ) // 标识结束访问状态,使日程size=0为结束的唯一标识
147 plan.pop_front();
148 if (currentTask.over && plan.size() > 1 ) // 仅当任务结束后更新任务
149 {
150 preTask = currentTask;
151 currentTask = updateTask();
152 }
153 room & rm = rom[currentTask.getRoomNum() - 1 ];
154 elevator & elev = elevt[currentTask.getFloorNum() - 1 ];
155 switch (prSt)
156 {
157 case s_Outside: // 前一状态:楼外
158 st.description = " Entry " ;
159 st.finish = beginTime + 30 ;
160 prSt = (currentTask.getFloorNum() == 1 ) ? s_InRoomQueue:s_InEleQueue; // 进入电梯队列或房间队列
161 break ;
162 case s_InEleQueue: // 前一状态:电梯队列
163 {
164 size_t i;
165 // 当前时间agent在电梯队列中的位置计算:
166 if (st.waiting) // 等待状态时,agent已在队列中
167 for (i = 0 ; i < elev.eleQueue.size() && elev.eleQueue[i] -> getPriority() != priority; ++ i) ;
168 else if (elev.eleQueue.size() == 0 ) // 非等待状态,队列为空,创建队列。
169 {
170 elev.eleQueue.push_front( this );
171 i = 0 ;
172 }
173 else // 非等待状态,队列非空,按优先级插入队列。
174 {
175 for (i = 0 ; i < elev.eleQueue.size() && elev.eleQueue[i] -> getPriority() < priority; ++ i) ;
176 elev.eleQueue.insert(elev.eleQueue.begin() + i, this );
177 }
178 if ( ! i &&! (beginTime < elev.freeUntil) && beginTime.second % 5 ) // 电梯每5秒打开,规格化begintime.
179 elev.freeUntil = beginTime + ( 5 - beginTime.second % 5 );
180 // agent在队首且电梯可用:
181 if ( ! i &&! (beginTime < elev.freeUntil) &&! st.waiting) // 不处于等待状态时进电梯
182 {
183 st.description = " Stay in elevator " ;
184 if (plan.size() == 0 ) // 访问结束下至01层
185 st.finish = beginTime + (currentTask.getFloorNum() - 1 ) * 30 ;
186 else st.finish = beginTime + abs(currentTask.getFloorNum() - preTask.getFloorNum()) * 30 ; // 至下一任务房间所在层
187 elev.freeUntil = beginTime + 5 ; // 更新电梯可用时间
188 elev.eleQueue.pop_front(); // 从电梯队列剔除
189 prSt = s_InElevator;
190 }
191 else if ( ! i &&! (beginTime < elev.freeUntil) && st.waiting) // 处于等待状态时,结束整个等待过程,更新state。
192 {
193 st.description = " Waiting in elevator queue " ;
194 elev.eleQueue.pop_front();
195 st.waiting = false ;
196 prSt = s_InEleQueue;
197 }
198 // agent在队首但电梯不可用,或agent不在队首,进入等待状态:
199 else
200 {
201 if ( ! i && beginTime < elev.freeUntil)
202 st.finish = elev.freeUntil;
203 else st.finish = elev.eleQueue[i - 1 ] -> getBegintime() + 5 ;
204 st.waiting = true ;
205 prSt = s_InEleQueue;
206 }
207 break ;
208 }
209 case s_InRoomQueue: // 前一状态:房间队列
210 {
211 size_t j;
212 // 当前时间agent在房间队列位置的计算:
213 if (st.waiting) // 等待状态时,agent已在队列中
214 for (j = 0 ; j < rm.roomQueue.size() && rm.roomQueue[j] -> getPriority() != priority; ++ j) ;
215 else if (rm.roomQueue.size() == 0 ) // 非等待,队列为空,创建队列
216 {
217 rm.roomQueue.push_front( this );
218 j = 0 ;
219 }
220 else // 非等待,队列非空,按优先级插入队列
221 {
222 for (j = 0 ; j < rm.roomQueue.size() && rm.roomQueue[j] -> getPriority() < priority; ++ j) ;
223 rm.roomQueue.insert(rm.roomQueue.begin() + j, this );
224 }
225 // agent在队首,且房间可用
226 if ( ! j &&! (beginTime < rm.freeUntil) &&! st.waiting) // 非等待状态时,进入房间
227 {
228 st.description = " Stay in room " + currentTask.xxyy;
229 st.finish = beginTime + currentTask.stay;
230 rm.roomQueue.pop_front(); // 从房间队列中剔除
231 rm.freeUntil = st.finish; // 更新房间可用时间
232 currentTask.over = true ; // 标记当前任务完成
233 prSt = s_InRoom;
234 }
235 else if ( ! j &&! (beginTime < rm.freeUntil) && st.waiting) // 处于等待状态时,结束整个等待过程,更新state。
236 {
237 st.description = " Waiting in front of room " + currentTask.xxyy;
238 st.waiting = false ;
239 rm.roomQueue.pop_front();
240 prSt = s_InRoomQueue;
241 }
242 // agent在队首但房间不可用,或agent不处于队首,进入等待状态
243 else
244 {
245 if ( ! j && beginTime < rm.freeUntil)
246 st.finish = rm.freeUntil;
247 else st.finish = rm.roomQueue[j - 1 ] -> getBegintime();
248 st.waiting = true ;
249 prSt = s_InRoomQueue;
250 }
251 break ;
252 }
253 case s_InElevator: // 前一状态:电梯内
254 if (plan.size() == 0 ) // 如果访问结束,exit。
255 {
256 st.description = " Exit " ;
257 st.finish = beginTime + 30 ;
258 prSt = s_PlanFinished;
259 }
260 else // 否则,进入房间队列。
261 {
262 st.description = " Transfer from elevator to room " + currentTask.xxyy;
263 st.finish = beginTime + 10 ;
264 prSt = s_InRoomQueue;
265 }
266 break ;
267 case s_InRoom: // 前一状态:房间内
268 if (plan.size() == 0 ) // 访问已结束
269 {
270 if (currentTask.getFloorNum() != 1 ) // 不在01层,进入电梯队列
271 {
272 st.description = " Transfer from room " + currentTask.xxyy + " to elevator " ;
273 st.finish = beginTime + 10 ;
274 prSt = s_InEleQueue;
275 }
276 else // 在01层,exit
277 {
278 st.description = " Exit " ;
279 st.finish = beginTime + 30 ;
280 prSt = s_PlanFinished;
281 }
282 }
283 else if (preTask.getFloorNum() != currentTask.getFloorNum()) // 下一任务房间不在同一楼层,进入电梯队列
284 {
285 st.description = " Transfer from room " + preTask.xxyy + " to elevator " ;
286 st.finish = beginTime + 10 ;
287 prSt = s_InEleQueue;
288 }
289 else // 下一任务房间在同一楼层,进入房间队列
290 {
291 st.description = " Transfer from room " + preTask.xxyy + " to room " + currentTask.xxyy;
292 st.finish = beginTime + 10 ;
293 prSt = s_InRoomQueue;
294 }
295 break ;
296 }
297 beginTime = st.finish;
298 return st;
299 }
300
301 void agent::visit(state & h)
302 {
303 if ( ! h.waiting)
304 route.push_back(h);
305 }
306 void agent::print()
307 {
308 printf( " %c\n " , priority);
309 for (size_t k = 0 ; k < route.size(); ++ k)
310 {
311 route[k].print();
312 }
313 printf( " \n " );
314 }
315 int getAgent(vector < agent >& a) // 按时间顺序获得当前待处理agent
316 {
317 int x =- 1 ;
318 int i;
319 for (i = 0 ; i < ( int )a.size() && a[i].getPreState() == s_PlanFinished; ++ i)
320 ;
321 if (i == ( int )a.size())
322 return x;
323 else
324 {
325 x = i;
326 for ( int j = x; j < ( int )a.size(); ++ j)
327 {
328 if (a[j].getPreState() != s_PlanFinished && a[j].getBegintime() < a[x].getBegintime())
329 x = j;
330 }
331 return x;
332 }
333 }
334 istream & operator >> (istream & is ,timmme & t)
335 {
336 char x;
337 is >> t.hour >> x >> t.minute >> x >> t.second;
338 return is ;
339 }
340 istream & operator >> (istream & is , task & r)
341 {
342 is >> r.xxyy;
343 if (r.xxyy == " 0 " )
344 return is ;
345 is >> r.stay;
346 return is ;
347 }
348 istream & operator >> (istream & is , agent & a)
349 {
350 task tsk;
351 is >> a.priority;
352 if (a.priority == ' . ' ) return is ;
353 is >> a.beginTime >> tsk;
354 while (tsk.xxyy != " 0 " )
355 {
356 a.plan.push_back(tsk);
357 is >> tsk;
358 }
359 return is ;
360 }
361 int main()
362 {
363 int k;
364 vector < room > rm( 100 );
365 vector < elevator > elevt( 10 );
366 vector < agent > agents;
367 agent theAgent;
368 cin >> theAgent;
369 while (theAgent.getPriority() != ' . ' )
370 {
371 agents.push_back(theAgent);
372 theAgent.plan.clear();
373 cin >> theAgent;
374 }
375
376 sort(agents.begin(), agents.end()); // 按优先级将agent排序以便于getAgent.
377 for (size_t i = 0 ; i < agents.size(); ++ i)
378 {
379 agents[i].SetCurrentTask();
380 }
381 k = getAgent(agents);
382 while (k + 1 )
383 {
384 state s = agents[k].getState(rm,elevt);
385 agents[k].visit(s);
386 k = getAgent(agents);
387 }
388 for (size_t i = 0 ; i < agents.size(); ++ i)
389 {
390 agents[i].print();
391 }
392 }
393
2 #include < string >
3 #include < iomanip >
4 #include < cmath >
5 #include < algorithm >
6 #include < deque >
7 #include < vector >
8 #include < time.h >
9 using namespace std;
10
11 enum preState
12 {
13 s_Outside,
14 s_InElevator,
15 s_InRoom,
16 s_InEleQueue,
17 s_InRoomQueue,
18 s_PlanFinished,
19 };
20 struct timmme
21 {
22 friend istream & operator >> (istream & ,timmme & );
23 int hour;
24 int minute;
25 int second;
26 timmme():hour( 0 ),minute( 0 ),second( 0 ){}
27 timmme( int hr, int mt, int sd):hour(hr),minute(mt),second(sd) {}
28 void print()
29 {
30 printf( " %02d:%02d:%02d " , hour, minute, second);
31 }
32 timmme operator + ( int stay)
33 {
34 timmme tm;
35 int tmp,tmp1;
36 tmp = (second + stay) / 60 ;
37 tm.second = (second + stay) % 60 ;
38 tmp1 = (minute + tmp) / 60 ;
39 tm.minute = (minute + tmp) % 60 ;
40 tm.hour = (hour + tmp1) % 24 ;
41 return tm;
42 }
43 bool operator < (timmme & rhs)
44 {
45 if (hour < rhs.hour)
46 return true ;
47 else if (hour > rhs.hour)
48 return false ;
49 else if (minute < rhs.minute)
50 return true ;
51 else if (minute > rhs.minute)
52 return false ;
53 else if (second < rhs.second)
54 return true ;
55 else return false ;
56 }
57 timmme & operator = ( const timmme & rhs)
58 {
59 second = rhs.second;
60 minute = rhs.minute;
61 hour = rhs.hour;
62 return * this ;
63 }
64 };
65 struct task
66 {
67 friend istream & operator >> (istream & , task & );
68 string xxyy;
69 int stay;
70 bool over;
71 int getRoomNum() // 获得房间号(1-100)
72 {
73 int a = (xxyy[ 2 ] == ' 1 ' ) ? 10 : int (xxyy[ 3 ] - ' 0 ' );
74 return (getFloorNum() - 1 ) * 10 + a;
75 }
76 int getFloorNum() // 获得楼层号(1-10)
77 {
78 return (xxyy[ 0 ] == ' 1 ' ) ? 10 : int (xxyy[ 1 ] - ' 0 ' );
79 }
80 task( string xy, int st):xxyy(xy),stay(st), over( false ){}
81 task():xxyy( " 0101 " ),stay( 0 ), over( false ){}
82 };
83 struct state
84 {
85 timmme start;
86 timmme finish;
87 bool waiting;
88 string description;
89 state():waiting( false ){}
90 void print()
91 {
92 start.print();
93 printf( " " );
94 finish.print();
95 printf( " %s\n " , description.c_str());
96 }
97 };
98 class agent;
99 struct room
100 {
101 timmme freeUntil;
102 deque < agent *> roomQueue;
103 };
104 struct elevator
105 {
106 timmme freeUntil;
107 deque < agent *> eleQueue;
108 };
109 class agent
110 {
111 friend istream & operator >> (istream & , agent & );
112 public :
113 state st;
114 deque < task > plan;
115 agent():prSt(s_Outside){}
116 void SetCurrentTask() { if (plan.size()) currentTask = plan.front(); }
117 task & updateTask();
118 char & getPriority() { return priority; }
119 timmme & getBegintime() { return beginTime; }
120 preState & getPreState() { return prSt; }
121 state getState(vector < room >& , vector < elevator >& );
122 void visit(state & );
123 void print();
124 bool operator < (agent & rhs)
125 {
126 return priority < rhs.getPriority();
127 }
128 private :
129 char priority;
130 timmme beginTime;
131 task currentTask;
132 task preTask;
133 preState prSt;
134 vector < state > route;
135 };
136 task & agent::updateTask() // 更新当前任务
137 {
138 plan.pop_front();
139 return plan.front();
140 }
141
142 state agent::getState(vector < room >& rom, vector < elevator >& elevt)
143 {
144 if ( ! st.waiting) // 仅不处于等待状态的agent才更新state起始时间
145 st.start = beginTime;
146 if (currentTask.over && plan.size() == 1 ) // 标识结束访问状态,使日程size=0为结束的唯一标识
147 plan.pop_front();
148 if (currentTask.over && plan.size() > 1 ) // 仅当任务结束后更新任务
149 {
150 preTask = currentTask;
151 currentTask = updateTask();
152 }
153 room & rm = rom[currentTask.getRoomNum() - 1 ];
154 elevator & elev = elevt[currentTask.getFloorNum() - 1 ];
155 switch (prSt)
156 {
157 case s_Outside: // 前一状态:楼外
158 st.description = " Entry " ;
159 st.finish = beginTime + 30 ;
160 prSt = (currentTask.getFloorNum() == 1 ) ? s_InRoomQueue:s_InEleQueue; // 进入电梯队列或房间队列
161 break ;
162 case s_InEleQueue: // 前一状态:电梯队列
163 {
164 size_t i;
165 // 当前时间agent在电梯队列中的位置计算:
166 if (st.waiting) // 等待状态时,agent已在队列中
167 for (i = 0 ; i < elev.eleQueue.size() && elev.eleQueue[i] -> getPriority() != priority; ++ i) ;
168 else if (elev.eleQueue.size() == 0 ) // 非等待状态,队列为空,创建队列。
169 {
170 elev.eleQueue.push_front( this );
171 i = 0 ;
172 }
173 else // 非等待状态,队列非空,按优先级插入队列。
174 {
175 for (i = 0 ; i < elev.eleQueue.size() && elev.eleQueue[i] -> getPriority() < priority; ++ i) ;
176 elev.eleQueue.insert(elev.eleQueue.begin() + i, this );
177 }
178 if ( ! i &&! (beginTime < elev.freeUntil) && beginTime.second % 5 ) // 电梯每5秒打开,规格化begintime.
179 elev.freeUntil = beginTime + ( 5 - beginTime.second % 5 );
180 // agent在队首且电梯可用:
181 if ( ! i &&! (beginTime < elev.freeUntil) &&! st.waiting) // 不处于等待状态时进电梯
182 {
183 st.description = " Stay in elevator " ;
184 if (plan.size() == 0 ) // 访问结束下至01层
185 st.finish = beginTime + (currentTask.getFloorNum() - 1 ) * 30 ;
186 else st.finish = beginTime + abs(currentTask.getFloorNum() - preTask.getFloorNum()) * 30 ; // 至下一任务房间所在层
187 elev.freeUntil = beginTime + 5 ; // 更新电梯可用时间
188 elev.eleQueue.pop_front(); // 从电梯队列剔除
189 prSt = s_InElevator;
190 }
191 else if ( ! i &&! (beginTime < elev.freeUntil) && st.waiting) // 处于等待状态时,结束整个等待过程,更新state。
192 {
193 st.description = " Waiting in elevator queue " ;
194 elev.eleQueue.pop_front();
195 st.waiting = false ;
196 prSt = s_InEleQueue;
197 }
198 // agent在队首但电梯不可用,或agent不在队首,进入等待状态:
199 else
200 {
201 if ( ! i && beginTime < elev.freeUntil)
202 st.finish = elev.freeUntil;
203 else st.finish = elev.eleQueue[i - 1 ] -> getBegintime() + 5 ;
204 st.waiting = true ;
205 prSt = s_InEleQueue;
206 }
207 break ;
208 }
209 case s_InRoomQueue: // 前一状态:房间队列
210 {
211 size_t j;
212 // 当前时间agent在房间队列位置的计算:
213 if (st.waiting) // 等待状态时,agent已在队列中
214 for (j = 0 ; j < rm.roomQueue.size() && rm.roomQueue[j] -> getPriority() != priority; ++ j) ;
215 else if (rm.roomQueue.size() == 0 ) // 非等待,队列为空,创建队列
216 {
217 rm.roomQueue.push_front( this );
218 j = 0 ;
219 }
220 else // 非等待,队列非空,按优先级插入队列
221 {
222 for (j = 0 ; j < rm.roomQueue.size() && rm.roomQueue[j] -> getPriority() < priority; ++ j) ;
223 rm.roomQueue.insert(rm.roomQueue.begin() + j, this );
224 }
225 // agent在队首,且房间可用
226 if ( ! j &&! (beginTime < rm.freeUntil) &&! st.waiting) // 非等待状态时,进入房间
227 {
228 st.description = " Stay in room " + currentTask.xxyy;
229 st.finish = beginTime + currentTask.stay;
230 rm.roomQueue.pop_front(); // 从房间队列中剔除
231 rm.freeUntil = st.finish; // 更新房间可用时间
232 currentTask.over = true ; // 标记当前任务完成
233 prSt = s_InRoom;
234 }
235 else if ( ! j &&! (beginTime < rm.freeUntil) && st.waiting) // 处于等待状态时,结束整个等待过程,更新state。
236 {
237 st.description = " Waiting in front of room " + currentTask.xxyy;
238 st.waiting = false ;
239 rm.roomQueue.pop_front();
240 prSt = s_InRoomQueue;
241 }
242 // agent在队首但房间不可用,或agent不处于队首,进入等待状态
243 else
244 {
245 if ( ! j && beginTime < rm.freeUntil)
246 st.finish = rm.freeUntil;
247 else st.finish = rm.roomQueue[j - 1 ] -> getBegintime();
248 st.waiting = true ;
249 prSt = s_InRoomQueue;
250 }
251 break ;
252 }
253 case s_InElevator: // 前一状态:电梯内
254 if (plan.size() == 0 ) // 如果访问结束,exit。
255 {
256 st.description = " Exit " ;
257 st.finish = beginTime + 30 ;
258 prSt = s_PlanFinished;
259 }
260 else // 否则,进入房间队列。
261 {
262 st.description = " Transfer from elevator to room " + currentTask.xxyy;
263 st.finish = beginTime + 10 ;
264 prSt = s_InRoomQueue;
265 }
266 break ;
267 case s_InRoom: // 前一状态:房间内
268 if (plan.size() == 0 ) // 访问已结束
269 {
270 if (currentTask.getFloorNum() != 1 ) // 不在01层,进入电梯队列
271 {
272 st.description = " Transfer from room " + currentTask.xxyy + " to elevator " ;
273 st.finish = beginTime + 10 ;
274 prSt = s_InEleQueue;
275 }
276 else // 在01层,exit
277 {
278 st.description = " Exit " ;
279 st.finish = beginTime + 30 ;
280 prSt = s_PlanFinished;
281 }
282 }
283 else if (preTask.getFloorNum() != currentTask.getFloorNum()) // 下一任务房间不在同一楼层,进入电梯队列
284 {
285 st.description = " Transfer from room " + preTask.xxyy + " to elevator " ;
286 st.finish = beginTime + 10 ;
287 prSt = s_InEleQueue;
288 }
289 else // 下一任务房间在同一楼层,进入房间队列
290 {
291 st.description = " Transfer from room " + preTask.xxyy + " to room " + currentTask.xxyy;
292 st.finish = beginTime + 10 ;
293 prSt = s_InRoomQueue;
294 }
295 break ;
296 }
297 beginTime = st.finish;
298 return st;
299 }
300
301 void agent::visit(state & h)
302 {
303 if ( ! h.waiting)
304 route.push_back(h);
305 }
306 void agent::print()
307 {
308 printf( " %c\n " , priority);
309 for (size_t k = 0 ; k < route.size(); ++ k)
310 {
311 route[k].print();
312 }
313 printf( " \n " );
314 }
315 int getAgent(vector < agent >& a) // 按时间顺序获得当前待处理agent
316 {
317 int x =- 1 ;
318 int i;
319 for (i = 0 ; i < ( int )a.size() && a[i].getPreState() == s_PlanFinished; ++ i)
320 ;
321 if (i == ( int )a.size())
322 return x;
323 else
324 {
325 x = i;
326 for ( int j = x; j < ( int )a.size(); ++ j)
327 {
328 if (a[j].getPreState() != s_PlanFinished && a[j].getBegintime() < a[x].getBegintime())
329 x = j;
330 }
331 return x;
332 }
333 }
334 istream & operator >> (istream & is ,timmme & t)
335 {
336 char x;
337 is >> t.hour >> x >> t.minute >> x >> t.second;
338 return is ;
339 }
340 istream & operator >> (istream & is , task & r)
341 {
342 is >> r.xxyy;
343 if (r.xxyy == " 0 " )
344 return is ;
345 is >> r.stay;
346 return is ;
347 }
348 istream & operator >> (istream & is , agent & a)
349 {
350 task tsk;
351 is >> a.priority;
352 if (a.priority == ' . ' ) return is ;
353 is >> a.beginTime >> tsk;
354 while (tsk.xxyy != " 0 " )
355 {
356 a.plan.push_back(tsk);
357 is >> tsk;
358 }
359 return is ;
360 }
361 int main()
362 {
363 int k;
364 vector < room > rm( 100 );
365 vector < elevator > elevt( 10 );
366 vector < agent > agents;
367 agent theAgent;
368 cin >> theAgent;
369 while (theAgent.getPriority() != ' . ' )
370 {
371 agents.push_back(theAgent);
372 theAgent.plan.clear();
373 cin >> theAgent;
374 }
375
376 sort(agents.begin(), agents.end()); // 按优先级将agent排序以便于getAgent.
377 for (size_t i = 0 ; i < agents.size(); ++ i)
378 {
379 agents[i].SetCurrentTask();
380 }
381 k = getAgent(agents);
382 while (k + 1 )
383 {
384 state s = agents[k].getState(rm,elevt);
385 agents[k].visit(s);
386 k = getAgent(agents);
387 }
388 for (size_t i = 0 ; i < agents.size(); ++ i)
389 {
390 agents[i].print();
391 }
392 }
393