Under construction

Still being built. Things may change

Minimum Cost Roads

Time limit2sMemory limit1024 MB

Summary
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 NN intersections and MM roads. The ii-th road has length lil_i meters, costs cic_i dollars per year to maintain, and connects intersections uiu_i and viv_i. To create a plan, Alanna must select a subset of the MM 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 (i,j)(i, j), if there is a path from ii to jj of length ll meters or less in the current road plan, then Alanna's plan must also contain a path between ii and jj of length at most ll meters. Any plan that breaks this rule is rejected.

Input

The first line contains the integers NN and MM.

Each of the next MM lines contains the integers uiu_i, viv_i, lil_i, and cic_i, meaning that there is a road from intersection uiu_i to intersection viv_i with length lil_i and cost cic_i (1≤ui,vi≤N,ui≠vi)(1 \le u_i, v_i \le N, u_i \neq v_i).

Output

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

Constraints

1≤N,M≤2 0001 \le N, M \le 2\,000

0≤li≤1090 \le l_i \le 10^9

1≤ci≤1091 \le c_i \le 10^9

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    25