Minimum Cost Roads
Time limit2sMemory limit1024 MB
Given a weighted undirected road graph, keep a subset of roads with minimum total cost so every pair's shortest distance stays unchanged.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Greedy
- Solved
- No attempts yet
Problem
As the newly elected mayor of Kitchener, Alanna's first job is to improve the city's road plan.
Kitchener's current road plan is a collection of intersections and roads. The -th road has length meters, costs dollars per year to maintain, and connects intersections and . To create a plan, Alanna must select a subset of the roads to keep and maintain. The cost of a plan is the sum of the maintenance costs of the roads in that subset.
To lower the city's annual spending, Alanna wants to minimize the plan's cost. The city also requires that travel distances stay short. Formally, for any pair of intersections , if there is a path from to of length meters or less in the current road plan, then Alanna's plan must also contain a path between and of length at most meters. Any plan that breaks this rule is rejected.
Input
The first line contains the integers and .
Each of the next lines contains the integers , , , and , meaning that there is a road from intersection to intersection with length and cost .
Output
Output one integer, the minimum possible cost of a road plan that meets the requirements.
Constraints