#P9985. [USACO23DEC] Train Scheduling P

    ID: 11327 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DPUSACO2023O2优化动态规划优化

[USACO23DEC] Train Scheduling P

Background

Note: The memory limit for this problem is 512MB, twice the default.

Problem Description

Bessie has found a new job in train scheduling. There are now two train stations, AA and BB. Due to budget limits, there is only a single-track railway connecting station AA and station BB. If a train leaves one of the stations at time tt, it will arrive at the other station at time t+Tt+T (1T10121 \le T \le 10^{12}).

Now there are NN (1N50001 \le N \le 5000) trains whose departure times need to be scheduled. Train ii must depart from station sis_i after time tit_i (si{A,B}s_i\in \{A,B\}, 0ti10120 \le t_i \le 10^{12}). Trains traveling in opposite directions are not allowed to be on the track at the same time, or they will collide. However, assume trains have negligible size, so at the same time the track can contain many trains traveling in the same direction.

Help Bessie schedule the departure time for each train, minimizing the total delay while avoiding collisions. Suppose train ii is scheduled to depart at time aia_i. The total delay is i=1naiti\sum\limits_{i=1}^n{a_i-t_i}.

Input Format

The first line contains NN and TT.

The next NN lines each describe one train. Line ii contains si,tis_i, t_i for train ii.

Output Format

Output the minimum total delay among all valid schedules.

1 95
B 63
0
4 1
B 3
B 2
A 1
A 3
1
4 10
A 1
B 2
A 3
A 21
13
8 125000000000
B 17108575619
B 57117098303
A 42515717584
B 26473500855
A 108514697534
B 110763448122
B 117731666682
A 29117227954
548047356974

Hint

Sample Explanation 1

The only train departs on time.

Sample Explanation 2

There are two optimal schedules. In the first, trains 2,3,42,3,4 depart on time, and train 11 departs with a 11-minute delay. In the second, trains 1,2,31,2,3 depart on time, and train 44 departs with a 11-minute delay.

Sample Explanation 3

The optimal schedule is: trains 1,31,3 depart on time, train 22 departs at time 1313, and train 44 departs at time 2323. The total delay is 0+11+0+2=130+11+0+2=13.

Test Point Properties

  • Test points 565-6 satisfy N15N \le 15.
  • Test points 7107-10 satisfy N100N \le 100.
  • Test points 111411-14 satisfy N500N \le 500.
  • Test points 151815-18 satisfy N2000N \le 2000.
  • Test points 192419-24 have no additional constraints.

Translated by ChatGPT 5