#P3931. SAC E#1 - 一道难题 Tree

    ID: 2871 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp网络流洛谷原创最大流最小割洛谷月赛

SAC E#1 - 一道难题 Tree

Description

To protect Gensokyo, 冴月麟 created a reflection of Gensokyo and sealed away the real Gensokyo. No one can enter the real Gensokyo anymore, but she left a clue for 魏潇承 who came to save her.

She set up a rooted tree. Each edge has a cost to cut it.

魏潇承 needs to compute the minimum cost to cut this tree. This is the little secret agreed upon by 冴月麟 and 魏潇承.

Please help 魏潇承.

Note: To “cut open” a rooted tree means to delete some edges so that no leaf node is connected to the root.

Input Format

The first line contains two integers n,rootn, \mathrm{root}, representing the number of nodes in the tree and the root.

Each of the next n1n-1 lines contains three integers a,b,ca, b, c, indicating there is an edge between aa and bb with cost cc.

Output Format

Output one line containing a single integer, the minimum cost.

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

Hint

Constraints and Agreements

  • For 20%20\% of the testdata, n10n \le 10.
  • For 50%50\% of the testdata, n1000n \le 1000.
  • For 100%100\% of the testdata, 2n1000002 \le n \le 100000, and edge weights are non-negative integers not exceeding 10610^6.

Translated by ChatGPT 5