Bruce's Brittle Bridges

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 2.0s
Memory limit: 256M

Authors:
Problem type

It is a well known fact that Sam is an incredibly talented programmer. However, his teacher Bruce is beginning to have doubts.

To test his suspicions, Bruce drops Sam off in a faraway island of N connected islands, joined by N1 bidirectional bridges. However, the bridges are brittle, so bridge i can only be traversed a maximum of ti times. Moreover, the ith of the N islands houses a treasure chest of value vi.

Right before Bruce leaves to teach Sam's class without him, he tells Sam that he has one chance to tell him the maximum value of treasure chests he can obtain. If he fails to do so, Bruce will leave Sam stranded on the island forever. However, Sam is unable to solve this problem. He passes it on to Daniel, who passes it on to you to solve!

Give that Sam starts at island 1 and can end at any island, help Daniel find the right answer for Sam!

Constraints

For all subtasks:

1vi,ti109

1Ai,BiN

Subtask 1 [20%]

1N8

Subtask 2 [30%]

1N2×103

Subtask 3 [50%]

1N2×105

Input Specification

The first line of input will contain the integer N, the number of islands.

The second line of input will contain N positive integers vi.

The next N1 lines will contain Ai, Bi, and ti, representing a bidirectional edge between Ai and Bi that can be traversed a maximum of ti times.

Output Specification

Output one integer representing the maximum amount of treasure Sam can gather. Please note that 64-bit integers may be required for full marks.

Sample Input 1

Copy
3
4 8 5
1 2 2
2 3 2

Sample Output 1

Copy
17

Explanation for Sample Output 1

Sam's optimal path is 123, gathering 4+8+5=17 units of treasure. Note that Sam is able to return to his starting position, but this is not required for this case.

Sample Input 2

Copy
5
8 10 4 9 1
1 2 1
1 3 2
2 4 1
2 5 1

Sample Output 2

Copy
31

Explanation for Sample Output 2

Sam's optimal path is 13124, gathering 8+4+0+10+9=31 units of treasure. Note that since each treasure chest can only be retrieved once, returning to island 1 doesn't grant additional treasure.


Comments


  • 20
    DM__Oscar  commented on March 3, 2021, 12:13 a.m.

    Let's go stuck on USACO silver gang!

    :blobweary: