#P6213. 「SWTR-4」Collecting Coins
「SWTR-4」Collecting Coins
Background
Little A likes Collecting Coins. He also has a good friend named little c.
While traveling outside, little c got trapped in a maze. After hearing the news, little A immediately put down the “tree-of-trees-of-trees” he was playing and set off to rescue her.
Problem Description
After some investigation, little A found that the maze where little c is trapped consists of rooms, connected by doors, forming a tree. Little c is trapped in room .
Little A also found that each door has a number written on it. Passing through that door gives coins, but the coins on each door can only be obtained once.
Since the bad guys who trapped little c already knew little A would come to rescue her, they placed traps in every room, so that room can be entered at most times; otherwise, little A would also be trapped in the maze. Luckily, when asking little A for help, little c had already told him about this trap.
When entering the maze, little A may choose any starting room (entering the maze counts as entering room once). Little A can leave the maze if and only if he is in room .
If little A enters room , we consider that he has successfully rescued little c. After rescuing little c, little A may continue moving in the maze with her.
Although little A is not a very greedy person, he still wants to know: under the condition that he successfully rescues little c and leaves the maze, what is the maximum number of coins he can obtain.
Input Format
The first line contains two integers , representing the number of rooms and the room number where little c is trapped.
The next lines each contain three integers , representing the two room numbers connected by the -th door, and the number written on that door.
The next line contains integers , representing the maximum number of times each room can be entered.
Output Format
Output one integer in a single line, representing the maximum number of coins little A can obtain.
6 4
1 2 3
2 3 4
2 4 5
3 5 2
3 6 3
1 2 1 1 2 2
5
6 4
1 2 3
2 3 4
2 4 5
3 5 2
3 6 3
1 1 1 1 1 1
0
6 4
1 2 3
2 3 4
2 4 5
3 5 2
3 6 3
2 2 2 2 2 2
12
12 6
1 4 33
2 11 51
3 9 100
4 8 7
5 9 35
6 12 30
7 11 58
8 10 65
9 10 59
10 12 9
11 12 72
2 2 2 3 2 1 2 1 2 3 2 2
263
Hint
[Sample Explanation]

One optimal route is: , and it can obtain coins in total.
[Sample Explanation]
As in the figure above, little A can only “airdrop” to room , and then leave the maze, obtaining coins in total.
[Sample Explanation]

One optimal route is: $3\to 9\to 10\to 8\to 10\to 12\to 6\to 12\to 10\to 9\to 3$, and it can obtain coins in total.
[Constraints and Notes]
This problem uses bundled testdata.
| Subtask ID | Special Property | Score | |
|---|---|---|---|
| The maze is a chain | |||
| None | |||
| The maze is a chain | |||
| The maze is a “chrysanthemum graph” (juhua tu) | |||
| None |
For of the data, , .
[Source]
idea & std: Alex_Wei, validator: chenxia25
Translated by ChatGPT 5