ADDP-P2

The most parcels collected

Practice 2 of 6Advanced DP17 tests

The problem

A delivery van starts at junction s of a town with one-way streets. At each junction v, value[v] parcels are waiting. The van may drive along the streets for as long as it likes, through any junction any number of times, and it collects the parcels of every junction it passes, the first time it is there. What is the largest number of parcels it can collect?

most_collected(3, [(0, 1), (1, 0), (0, 2)], [0, 5, 7], 0)   # 12
most_collected(2, [(0, 1)], [3, 4], 1)                       # 4

In the first, the van goes to 1, back to 0, then on to 2. Streets may repeat, and a street may lead from a junction to itself. Values are 0 or more.

Limits

  • Up to 100,000 junctions and 150,000 streets.
  • A chain of 100,000 streets in a row: searches must not use recursion.

Builds on ADDP-2 DP on DAGs.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

17 tests, not run yet
Test results
#TestResultReads · time
1Just the startNot run
2One streetNot run
3No way backNot run
4A loopNot run
5Into a loop and out againNot run
6Choose the better branchNot run
7Each junction counts onceNot run
8Out of reachNot run
9Repeated streets and a street to itselfNot run
10Greedy goes wrongNot run
11Two loops joinedNot run
12A loop with two exitsNot run
13A tangle of 30Not run
14A one-way chain of 100,000Not run
15A ring of 100,000Not run
16A chain of 30,000 loopsNot run
1760,000 junctions, randomNot run