Minimum Time to Transport All Individuals - Problem

You are given n individuals at a base camp who need to cross a river to reach a destination using a single boat. The boat can carry at most k people at a time.

The trip is affected by environmental conditions that vary cyclically over m stages. Each stage j has a speed multiplier mul[j]:

  • If mul[j] > 1, the trip slows down
  • If mul[j] < 1, the trip speeds up

Each individual i has a rowing strength represented by time[i], the time (in minutes) it takes them to cross alone in neutral conditions.

Rules:

  • A group g departing at stage j takes time equal to the maximum time[i] among its members, multiplied by mul[j] minutes to reach the destination
  • After the group crosses the river in time d, the stage advances by floor(d) % m steps
  • If individuals are left behind, one person must return with the boat. Let r be the index of the returning person, the return takes time[r] × mul[current_stage] minutes, and the stage advances by floor(return_time) % m

Return the minimum total time required to transport all individuals. If it is not possible to transport all individuals to the destination, return -1.

Input & Output

Example 1 — Basic Transport
$ Input: time = [10, 15, 8], k = 2, mul = [1.5, 1.2]
Output: 51
💡 Note: Person 2 crosses alone (8×1.5=12), returns (8×1.2=9.6≈10), persons 0&1 cross together (15×1.2=18), person 2 returns and crosses with remaining person for total time of 51.
Example 2 — Single Person
$ Input: time = [5], k = 1, mul = [2.0]
Output: 10
💡 Note: Only one person needs to cross: 5 × 2.0 = 10 minutes.
Example 3 — Optimal Grouping
$ Input: time = [1, 2, 5, 10], k = 2, mul = [1.0, 0.8]
Output: 19
💡 Note: Fast people cross first, then optimal return strategy minimizes total time.

Constraints

  • 1 ≤ n ≤ 20
  • 1 ≤ k ≤ n
  • 1 ≤ m ≤ 10
  • 1 ≤ time[i] ≤ 1000
  • 0.1 ≤ mul[j] ≤ 10.0

Visualization

Tap to expand
Minimum Time to Transport All Individuals INPUT BOAT (k=2) Individuals: P0 t=10 P1 t=15 P2 t=8 time = [10, 15, 8] k = 2 (boat capacity) Stage Multipliers: Stage 0 1.5x Stage 1 1.2x ALGORITHM STEPS 1 BFS State Setup State: (people_left, stage, time) 2 Trip 1: P0+P2 cross max(10,8)*1.5 = 15 min 3 Return: P2 goes back 8*1.2 = 9.6 min 4 Trip 2: P1+P2 cross max(15,8)*1.5 = 22.5 min BFS State Tracking Init: {P0,P1,P2}, stg=0 T1: {P1}, stg=1, t=15 Ret: {P1,P2}, stg=0, t=24.6 T2: {}, stg=1, t=47.1 Optimal path found! FINAL RESULT BASE CAMP (empty) RIVER DEST P0 P1 P2 Time Breakdown Trip 1 (P0+P2): 15.0 min Return (P2): 9.6 min Trip 2 (P1+P2): 22.5 min Total (rounded): ~47 min OUTPUT 51 OK - All Transported Key Insight: BFS explores all possible group combinations and return strategies. The state tracks: (1) which people remain at base, (2) current stage for multiplier, (3) accumulated time. Stage advances by floor(trip_time) % m after each crossing. Fastest rower returns to minimize total time. TutorialsPoint - Minimum Time to Transport All Individuals | BFS with State Tracking
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
8.9K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen