CCC '23 S4 - Minimum Cost Roads

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 2.0s
Memory limit: 1G

Author:
Problem type
Canadian Computing Competition: 2023 Stage 1, Senior #4

As the newly elected mayor of Kitchener, Alanna's first job is to improve the city's road plan.

Kitchener's current road plan can be represented as a collection of N intersections with M roads, where the i-th road has length li meters, costs ci dollars per year to maintain, and connects intersections ui and vi. To create a plan, Alanna must select some subset of the M roads to keep and maintain, and that plan's cost is the sum of maintenance costs of all roads in that subset.

To lower the city's annual spending, Alanna would like to minimize the plan's cost. However, the city also requires that she minimizes travel distances between intersections and will reject any plan that does not conform to those rules. Formally, for any pair of intersections (i,j), if there exists a path from i to j taking l meters on the existing road plan, Alanna's plan must also include a path between those intersections that is at most l meters.

Input Specification

The first line contains the integers N and M.

Each of the next M lines contains the integers ui, vi, li, and ci, meaning that there currently exists a road from intersection ui to intersection vi with length li and cost ci (1ui,viN,uivi).

The following table shows how the available 15 marks are distributed:

Marks Awarded Bounds on N and M Bounds on li Bounds on ci Additional Constraints
3 marks 1N,M2000 li=0 1ci109 None
6 marks 1N,M2000 1li109 1ci109 There is at most one road between any unordered pair of intersections.
6 marks 1N,M2000 0li109 1ci109 None

Output Specification

Output one integer, the minimum possible cost of a road plan that meets the requirements.

Sample Input

Copy
5 7
1 2 15 1
2 4 9 9
5 2 5 6
4 5 4 4
4 3 3 7
1 3 2 7
1 4 2 1

Output for Sample Input

Copy
25

Explanation of Output for Sample Input

Here is a diagram of the intersections along with a valid road plan with minimum cost.

Each edge is labeled with a pair (l,c) denoting that it has length l meters and cost c dollars. Additionally, the roads that are part of the plan are highlighted, with a total cost of 7+1+6+7+4=25.

It can be shown that we cannot create a cheaper plan that also respects the city's requirements.


Comments


  • 1
    AndyZhang68  commented on Aug. 29, 2024, 8:55 p.m. edit 2

    It seems that it's possible for two intersections to have multiple roads connecting them and that the entire initial road plan does not have to be connected. I spent way too long trying to fix those...