Step 1 / 20 3 0 5 1 2 2 7 3 weights 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dp[mask] = (rides, remaining)
Bài toán: 4 4 4 người có cân nặng [ 3 , 5 , 2 , 7 ] [3, 5, 2, 7] [ 3 , 5 , 2 , 7 ] , thang máy sức chứa x = 8 x = 8 x = 8 . Tìm số chuyến ít nhất. Ta dùng QHĐ bitmask với 16 16 16 mask, mỗi mask biểu diễn tập con người đã được chở. d p [ m a s k ] dp[mask] d p [ ma s k ] lưu (số chuyến tối thiểu, sức chứa còn lại tối đa trong chuyến cuối).
Step 2 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dp[mask] = (rides, remaining)
Trường hợp cơ sở: d p [ 0 ] = ( 1 , 8 ) dp[0] = (1, 8) d p [ 0 ] = ( 1 , 8 ) . Chưa chở ai. Ta có 1 1 1 chuyến mở sẵn với đầy đủ sức chứa 8 8 8 . Mask 0 0 0 = nhị phân 0000 0000 0000 nghĩa là chưa chọn ai.
Step 3 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 dp[mask] = (rides, remaining) Thử thêm người 0 0 0 (nặng 3 3 3 ) vào mask 0000 0000 0000 . Cân nặng 3 ≤ 8 3 \leq 8 3 ≤ 8 còn lại, vừa chuyến hiện tại. d p [ 0001 ] = ( 1 , 8 − 3 ) = ( 1 , 5 ) dp[0001] = (1, 8-3) = (1, 5) d p [ 0001 ] = ( 1 , 8 − 3 ) = ( 1 , 5 ) . Một chuyến, còn trống 5 5 5 .
Step 4 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 dp[mask] = (rides, remaining) Thêm người 1 1 1 (nặng 5 5 5 ) vào mask 0000 0000 0000 . Cân nặng 5 ≤ 8 5 \leq 8 5 ≤ 8 , vừa. d p [ 0010 ] = ( 1 , 8 − 5 ) = ( 1 , 3 ) dp[0010] = (1, 8-5) = (1, 3) d p [ 0010 ] = ( 1 , 8 − 5 ) = ( 1 , 3 ) . Một chuyến, còn trống 3 3 3 .
Step 5 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 3 1,6 4 5 6 7 8 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 dp[mask] = (rides, remaining) Thêm người 2 2 2 (nặng 2 2 2 ) vào mask 0000 0000 0000 . Cân nặng 2 ≤ 8 2 \leq 8 2 ≤ 8 , vừa. d p [ 0100 ] = ( 1 , 8 − 2 ) = ( 1 , 6 ) dp[0100] = (1, 8-2) = (1, 6) d p [ 0100 ] = ( 1 , 8 − 2 ) = ( 1 , 6 ) .
Step 6 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 3 1,6 4 5 6 7 1,1 8 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 dp[mask] = (rides, remaining) Thêm người 3 3 3 (nặng 7 7 7 ) vào mask 0000 0000 0000 . Cân nặng 7 ≤ 8 7 \leq 8 7 ≤ 8 , vừa. d p [ 1000 ] = ( 1 , 8 − 7 ) = ( 1 , 1 ) dp[1000] = (1, 8-7) = (1, 1) d p [ 1000 ] = ( 1 , 8 − 7 ) = ( 1 , 1 ) . Đã tính xong tất cả trạng thái một người từ tập rỗng.
Step 7 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 5 6 7 1,1 8 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 dp[mask] = (rides, remaining) Xử lý mask 0001 = ( 1 , 5 ) 0001 = (1,5) 0001 = ( 1 , 5 ) . Thêm người 1 1 1 (nặng 5 5 5 ). 5 ≤ 5 5 \leq 5 5 ≤ 5 còn lại, vừa đủ! d p [ 0011 ] = ( 1 , 5 − 5 ) = ( 1 , 0 ) dp[0011] = (1, 5-5) = (1, 0) d p [ 0011 ] = ( 1 , 5 − 5 ) = ( 1 , 0 ) . Người 0 0 0 và 1 1 1 chung một chuyến, vừa khít sức chứa 3 + 5 = 8 3+5=8 3 + 5 = 8 .
Step 8 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 6 7 1,1 8 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 dp[mask] = (rides, remaining) Từ mask 0001 = ( 1 , 5 ) 0001 = (1,5) 0001 = ( 1 , 5 ) , thêm người 2 2 2 (nặng 2 2 2 ). 2 ≤ 5 2 \leq 5 2 ≤ 5 , vừa. d p [ 0101 ] = ( 1 , 5 − 2 ) = ( 1 , 3 ) dp[0101] = (1, 5-2) = (1, 3) d p [ 0101 ] = ( 1 , 5 − 2 ) = ( 1 , 3 ) . Người 0 0 0 và 2 2 2 chung chuyến (nặng 3 + 2 = 5 3+2=5 3 + 2 = 5 ).
Step 9 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 6 7 1,1 8 2,1 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW dp[mask] = (rides, remaining) Từ mask 0001 = ( 1 , 5 ) 0001 = (1,5) 0001 = ( 1 , 5 ) , thêm người 3 3 3 (nặng 7 7 7 ). 7 > 5 7 > 5 7 > 5 còn lại – không vừa! Mở chuyến mới. d p [ 1001 ] = ( 1 + 1 , 8 − 7 ) = ( 2 , 1 ) dp[1001] = (1+1, 8-7) = (2, 1) d p [ 1001 ] = ( 1 + 1 , 8 − 7 ) = ( 2 , 1 ) . Bây giờ cần 2 2 2 chuyến.
Step 10 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 7 1,1 8 2,1 9 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 dp[mask] = (rides, remaining) Từ mask 0010 = ( 1 , 3 ) 0010 = (1,3) 0010 = ( 1 , 3 ) , thêm người 2 2 2 (nặng 2 2 2 ). 2 ≤ 3 2 \leq 3 2 ≤ 3 , vừa. d p [ 0110 ] = ( 1 , 3 − 2 ) = ( 1 , 1 ) dp[0110] = (1, 3-2) = (1, 1) d p [ 0110 ] = ( 1 , 3 − 2 ) = ( 1 , 1 ) . Người 1 1 1 và 2 2 2 chung chuyến (nặng 5 + 2 = 7 5+2=7 5 + 2 = 7 ).
Step 11 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 7 1,1 8 2,1 9 2,1 10 11 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW dp[mask] = (rides, remaining) Từ mask 0010 = ( 1 , 3 ) 0010 = (1,3) 0010 = ( 1 , 3 ) , thêm người 3 3 3 (nặng 7 7 7 ). 7 > 3 7 > 3 7 > 3 , mở chuyến mới. d p [ 1010 ] = ( 2 , 8 − 7 ) = ( 2 , 1 ) dp[1010] = (2, 8-7) = (2, 1) d p [ 1010 ] = ( 2 , 8 − 7 ) = ( 2 , 1 ) . Người 1 1 1 và 3 3 3 mỗi người cần chuyến riêng.
Step 12 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 7 1,1 8 2,1 9 2,1 10 11 2,1 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW dp[mask] = (rides, remaining) Từ mask 0100 = ( 1 , 6 ) 0100 = (1,6) 0100 = ( 1 , 6 ) , thêm người 3 3 3 (nặng 7 7 7 ). 7 > 6 7 > 6 7 > 6 , mở chuyến mới. d p [ 1100 ] = ( 2 , 1 ) dp[1100] = (2, 1) d p [ 1100 ] = ( 2 , 1 ) . Lưu ý: cũng có thể đến từ mask 1000 1000 1000 +người 2 2 2 cho kết quả ( 2 , 6 ) (2, 6) ( 2 , 6 ) tốt hơn – ta sẽ cập nhật ngay sau.
Step 13 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 7 1,1 8 2,1 9 2,1 10 11 2,6 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER dp[mask] = (rides, remaining) Cập nhật! Từ mask 1000 = ( 1 , 1 ) 1000 = (1,1) 1000 = ( 1 , 1 ) , thêm người 2 2 2 (nặng 2 2 2 ). 2 > 1 2 > 1 2 > 1 , mở chuyến mới. Ứng viên = ( 2 , 8 − 2 ) = ( 2 , 6 ) = (2, 8-2) = (2, 6) = ( 2 , 8 − 2 ) = ( 2 , 6 ) . Vì 6 > 1 6 > 1 6 > 1 sức chứa còn lại và cùng số chuyến, d p [ 1100 ] dp[1100] d p [ 1100 ] cập nhật thành ( 2 , 6 ) (2, 6) ( 2 , 6 ) . Còn nhiều chỗ trống hơn nghĩa là còn chỗ cho người tiếp theo.
Step 14 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 2,6 7 1,1 8 2,1 9 2,1 10 11 2,6 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER Arrow from dp.cell[3] to dp.cell[7]: w[2]=2 NEW w[2]=2 NEW dp[mask] = (rides, remaining) Từ mask 0011 = ( 1 , 0 ) 0011 = (1,0) 0011 = ( 1 , 0 ) , thêm người 2 2 2 (nặng 2 2 2 ). 2 > 0 2 > 0 2 > 0 , mở chuyến mới. d p [ 0111 ] = ( 2 , 8 − 2 ) = ( 2 , 6 ) dp[0111] = (2, 8-2) = (2, 6) d p [ 0111 ] = ( 2 , 8 − 2 ) = ( 2 , 6 ) . Người 0 0 0 và 1 1 1 lấp đầy chuyến 1 1 1 ; người 2 2 2 bắt đầu chuyến 2 2 2 với 6 6 6 chỗ trống.
Step 15 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 2,6 7 1,1 8 2,1 9 2,1 10 2,0 11 2,6 12 13 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER Arrow from dp.cell[3] to dp.cell[7]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[3] to dp.cell[11]: w[3]=7 NEW w[3]=7 NEW dp[mask] = (rides, remaining) Đường tốt nhất đến mask 0011 = ( 1 , 0 ) 0011 = (1,0) 0011 = ( 1 , 0 ) , thêm người 3 3 3 (nặng 7 7 7 ). 7 > 0 7 > 0 7 > 0 , mở chuyến mới. d p [ 1011 ] = ( 2 , 8 − 7 ) = ( 2 , 1 ) dp[1011] = (2, 8-7) = (2, 1) d p [ 1011 ] = ( 2 , 8 − 7 ) = ( 2 , 1 ) . Cũng từ mask 0101 = ( 1 , 3 ) 0101=(1,3) 0101 = ( 1 , 3 ) +người 3 3 3 : 7 > 3 7>3 7 > 3 , cho ( 2 , 1 ) (2,1) ( 2 , 1 ) . Và từ mask 1001 = ( 2 , 1 ) 1001=(2,1) 1001 = ( 2 , 1 ) +người 1 1 1 : 5 > 1 5>1 5 > 1 , cho ( 3 , 3 ) (3,3) ( 3 , 3 ) . Tốt nhất: ( 2 , 1 ) (2,1) ( 2 , 1 ) . Tất cả các đường đều đồng ý 2 2 2 chuyến.
Step 16 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 2,6 7 1,1 8 2,1 9 2,1 10 2,0 11 2,6 12 13 2,0 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER Arrow from dp.cell[3] to dp.cell[7]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[3] to dp.cell[11]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[6] to dp.cell[14]: w[3]=7 NEW w[3]=7 NEW dp[mask] = (rides, remaining) Từ mask 0110 = ( 1 , 1 ) 0110 = (1,1) 0110 = ( 1 , 1 ) , thêm người 3 3 3 (nặng 7 7 7 ). 7 > 1 7 > 1 7 > 1 , mở chuyến mới. d p [ 1110 ] = ( 2 , 8 − 7 ) = ( 2 , 1 ) dp[1110] = (2, 8-7) = (2, 1) d p [ 1110 ] = ( 2 , 8 − 7 ) = ( 2 , 1 ) . Từ mask 1100 = ( 2 , 6 ) 1100=(2,6) 1100 = ( 2 , 6 ) +người 1 1 1 : 5 ≤ 6 5 \leq 6 5 ≤ 6 , vừa! Ứng viên ( 2 , 1 ) (2,1) ( 2 , 1 ) . Và từ mask 1010 = ( 2 , 1 ) 1010=(2,1) 1010 = ( 2 , 1 ) +người 2 2 2 : 2 > 1 2>1 2 > 1 , cho ( 3 , 6 ) (3,6) ( 3 , 6 ) . Tốt nhất: ( 2 , 1 ) (2, 1) ( 2 , 1 ) . Tất cả cho 2 2 2 chuyến.
Step 17 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 2,6 7 1,1 8 2,1 9 2,1 10 2,0 11 2,6 12 2,3 13 2,0 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER Arrow from dp.cell[3] to dp.cell[7]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[3] to dp.cell[11]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[6] to dp.cell[14]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[12] to dp.cell[13]: w[0]=3 w[0]=3 dp[mask] = (rides, remaining) d p [ 1101 ] dp[1101] d p [ 1101 ] = mask chứa người { 0 , 2 , 3 } \{0,2,3\} { 0 , 2 , 3 } . Đường tốt nhất: từ mask 0101 = ( 1 , 3 ) 0101=(1,3) 0101 = ( 1 , 3 ) +người 3 3 3 : 7 > 3 7>3 7 > 3 , cho ( 2 , 1 ) (2,1) ( 2 , 1 ) . Từ mask 1100 = ( 2 , 6 ) 1100=(2,6) 1100 = ( 2 , 6 ) +người 0 0 0 : 3 ≤ 6 3 \leq 6 3 ≤ 6 , cho ( 2 , 3 ) (2,3) ( 2 , 3 ) . Đường thứ hai tốt hơn – cùng 2 2 2 chuyến nhưng còn trống 3 3 3 thay vì 1 1 1 . d p [ 1101 ] = ( 2 , 3 ) dp[1101] = (2, 3) d p [ 1101 ] = ( 2 , 3 ) .
Step 18 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 2,6 7 1,1 8 2,1 9 2,1 10 2,0 11 2,6 12 2,3 13 2,0 14 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER Arrow from dp.cell[3] to dp.cell[7]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[3] to dp.cell[11]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[6] to dp.cell[14]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[12] to dp.cell[13]: w[0]=3 w[0]=3 Arrow from dp.cell[11] to dp.cell[15]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[7] to dp.cell[15]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[13] to dp.cell[15]: w[1]=5 NEW w[1]=5 NEW Arrow from dp.cell[14] to dp.cell[15]: w[0]=3 NEW w[0]=3 NEW dp[mask] = (rides, remaining) Cuối cùng, d p [ 1111 ] dp[1111] d p [ 1111 ] – cả 4 4 4 người. Ta kiểm tra chuyển trạng thái từ tất cả mask thiếu đúng một người. Từ d p [ 0111 ] = ( 2 , 6 ) dp[0111]=(2,6) d p [ 0111 ] = ( 2 , 6 ) +người 3 3 3 (w = 7 w=7 w = 7 ): 7 > 6 7>6 7 > 6 , chuyến mới, ứng viên ( 3 , 1 ) (3,1) ( 3 , 1 ) . Từ d p [ 1011 ] = ( 2 , 1 ) dp[1011]=(2,1) d p [ 1011 ] = ( 2 , 1 ) +người 2 2 2 (w = 2 w=2 w = 2 ): 2 > 1 2>1 2 > 1 , chuyến mới, ứng viên ( 3 , 6 ) (3,6) ( 3 , 6 ) . Từ d p [ 1101 ] = ( 2 , 3 ) dp[1101]=(2,3) d p [ 1101 ] = ( 2 , 3 ) +người 1 1 1 (w = 5 w=5 w = 5 ): 5 > 3 5>3 5 > 3 , chuyến mới, ứng viên ( 3 , 3 ) (3,3) ( 3 , 3 ) . Từ d p [ 1110 ] = ( 2 , 1 ) dp[1110]=(2,1) d p [ 1110 ] = ( 2 , 1 ) +người 0 0 0 (w = 3 w=3 w = 3 ): 3 > 1 3>1 3 > 1 , chuyến mới, ứng viên ( 3 , 3 ) (3,3) ( 3 , 3 ) .
Step 19 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 2,6 7 1,1 8 2,1 9 2,1 10 2,0 11 2,6 12 2,3 13 2,0 14 3,6 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER Arrow from dp.cell[3] to dp.cell[7]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[3] to dp.cell[11]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[6] to dp.cell[14]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[12] to dp.cell[13]: w[0]=3 w[0]=3 Arrow from dp.cell[11] to dp.cell[15]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[7] to dp.cell[15]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[13] to dp.cell[15]: w[1]=5 NEW w[1]=5 NEW Arrow from dp.cell[14] to dp.cell[15]: w[0]=3 NEW w[0]=3 NEW dp[mask] = (rides, remaining) Cả bốn chuyển trạng thái đều cho 3 3 3 chuyến. Sức chứa còn lại tốt nhất là 6 6 6 (từ d p [ 1011 ] dp[1011] d p [ 1011 ] +người 2 2 2 ). d p [ 1111 ] = ( 3 , 6 ) dp[1111] = (3, 6) d p [ 1111 ] = ( 3 , 6 ) . Nhưng đáp án chỉ là số chuyến: 3 3 3 .
Step 20 / 20 3 0 5 1 2 2 7 3 weights 1,8 0 1,5 1 1,3 2 1,0 3 1,6 4 1,3 5 1,1 6 2,6 7 1,1 8 2,1 9 2,1 10 2,0 11 2,6 12 2,3 13 2,0 14 3,6 15 Arrow from dp.cell[0] to dp.cell[1]: w[0]=3 w[0]=3 Arrow from dp.cell[0] to dp.cell[2]: w[1]=5 w[1]=5 Arrow from dp.cell[0] to dp.cell[4]: w[2]=2 w[2]=2 Arrow from dp.cell[0] to dp.cell[8]: w[3]=7 w[3]=7 Arrow from dp.cell[1] to dp.cell[3]: w[1]=5 w[1]=5 Arrow from dp.cell[1] to dp.cell[5]: w[2]=2 w[2]=2 Arrow from dp.cell[1] to dp.cell[9]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[2] to dp.cell[6]: w[2]=2 w[2]=2 Arrow from dp.cell[2] to dp.cell[10]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[4] to dp.cell[12]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[8] to dp.cell[12]: w[2]=2 BETTER w[2]=2 BETTER Arrow from dp.cell[3] to dp.cell[7]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[3] to dp.cell[11]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[6] to dp.cell[14]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[12] to dp.cell[13]: w[0]=3 w[0]=3 Arrow from dp.cell[11] to dp.cell[15]: w[2]=2 NEW w[2]=2 NEW Arrow from dp.cell[7] to dp.cell[15]: w[3]=7 NEW w[3]=7 NEW Arrow from dp.cell[13] to dp.cell[15]: w[1]=5 NEW w[1]=5 NEW Arrow from dp.cell[14] to dp.cell[15]: w[0]=3 NEW w[0]=3 NEW dp[mask] = (rides, remaining) Đáp án: d p [ 1111 ] . s o ˆ ˊ _chuy e ˆ ˊ n = 3 dp[1111].\text{số\_chuyến} = 3 d p [ 1111 ] . s o ˆ ˊ _chuy e ˆ ˊ n = 3 . Số chuyến thang máy tối thiểu cho cân nặng [ 3 , 5 , 2 , 7 ] [3, 5, 2, 7] [ 3 , 5 , 2 , 7 ] với sức chứa 8 8 8 là 3 3 3 . Một cách phân nhóm tối ưu: chuyến 1 1 1 = { \{ { người 1 1 1 , người