Pimp My Ride
Time limit1sMemory limit128 MB
Given n jobs with base prices and pairwise surcharges paid when a later job follows an earlier one, find the cheapest order to finish all jobs.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
There are plenty of cars, motorcycles, trucks, and other vehicles on the streets that badly need refurbishment. You have taken on the job of restoring them, funded by a generous budget from a major TV station.
There is a lot of work to do, so you decide to hand off the individual tasks — painting, interior decoration, and so on — to specialized garages. Because each garage handles only certain kinds of work, different tasks go to different garages. Worse, a garage tends to charge more when the car already looks good: a painter, for example, may charge extra for a car whose interior is already all leather. Since these surcharges depend on which jobs have already been completed, you want to save money by finding an optimal order in which to perform the jobs.
The jobs are numbered through . Each job has a base price, and for every ordered pair of jobs with there is a surcharge (in US dollars): you must pay an additional for job if and only if job was completed before job . The total cost of a job order is the sum, over all jobs, of each job's base price plus the surcharges owed for the jobs already finished at the moment it is performed. Compute the minimum possible total cost to finish all jobs.
Input
The first line contains the number of scenarios.
Each scenario starts with a line containing the number of jobs (). The next lines describe the cost matrix, each containing exactly integers. On line (for ), the -th integer is the base price of job , and the -th integer (for ) is the surcharge for job that applies if job was completed before it. Every price and surcharge is a non-negative integer not exceeding .
Output
For each scenario, first print a line:
Scenario #i:
where is the scenario number, counting from . Then print a line:
You have officially been pimped for only $p
where is the minimum total cost. Print a blank line between consecutive scenarios.