#P6869. [COCI 2019/2020 #5] Putovanje

[COCI 2019/2020 #5] Putovanje

Problem Description

You are given a tree with nn nodes, numbered from 11 to nn.

You will visit each node in increasing order of its label.

Traversing edges in the tree costs money. For the ii-th edge, there is a single-use ticket (can only be used once) with price ci1c_{i1}, and a multi-use ticket (can be used an unlimited number of times) with price ci2c_{i2}. During your trip, you may traverse an edge multiple times, so a multi-use ticket can sometimes be more cost-effective.

Please find the minimum total cost required to visit from 11 to nn.

Input Format

  • The first line: a positive integer nn.

  • The next n1n-1 lines describe the n1n-1 edges: four positive integers ai,bi,ci1,ci2a_i, b_i, c_{i1}, c_{i2}, meaning there is an edge connecting aia_i and bib_i whose single-use ticket costs ci1c_{i1} and multi-use ticket costs ci2c_{i2}.

Output Format

Output one line with one positive integer: your answer.

4
1 2 3 5
1 3 2 4
2 4 1 3

10
4
1 4 5 5
3 4 4 7
2 4 2 6

16
5
1 2 2 3
1 3 2 3
1 4 2 3
1 5 2 3

11

Hint

Sample #1 Explanation

  • 121\to 2: multi-use ticket, cost 55.
  • 2132\to 1\to 3: 212\to 1 uses the previously bought multi-use ticket, no cost; 131\to 3 single-use ticket, cost 22.
  • 31243\to 1\to 2\to 4: 313\to 1 single-use ticket, cost 22; 121\to 2 uses the previously bought multi-use ticket, no cost; 242\to 4 single-use ticket, cost 11.
  • Total cost is 5+2+2+1=105+2+2+1=10.

Constraints

This problem uses bundled testdata.

  • For 20pts20 pts of the testdata, 2n20002\leq n\leq 2000.

  • For another 25pts25 pts of the testdata, each town is directly connected to at most two other towns.

  • For all testdata, 2n2×1052\leq n\leq 2\times10^5, 1ai,bin1\leq a_i, b_i\leq n, 1ci1ci21051\leq c_{i1}\leq c_{i2}\leq 10^5.

Notes

Translated from COCI2019-2020 CONTEST #5 T4 Putovanje.

Translated by ChatGPT 5