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

    ID: 7270 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2019差分COCI(克罗地亚)

[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 n−1n-1 lines describe the n−1n-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

  • 1→21\to 2: multi-use ticket, cost 55.
  • 2→1→32\to 1\to 3: 2→12\to 1 uses the previously bought multi-use ticket, no cost; 1→31\to 3 single-use ticket, cost 22.
  • 3→1→2→43\to 1\to 2\to 4: 3→13\to 1 single-use ticket, cost 22; 1→21\to 2 uses the previously bought multi-use ticket, no cost; 2→42\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, 2≤n≤20002\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, 2≤n≤2×1052\leq n\leq 2\times10^5, 1≤ai,bi≤n1\leq a_i, b_i\leq n, 1≤ci1≤ci2≤1051\leq c_{i1}\leq c_{i2}\leq 10^5.

Notes

Translated from COCI2019-2020 CONTEST #5 T4 Putovanje.

Translated by ChatGPT 5