Treasure Hunt
Time limit2sMemory limit1024 MB
Find the maximum treasure sum on a walk from room 1 in a directed graph where pulling a lever in room a adds a two-way passage between x and y.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Phoenix finally has a treasure map. Phoenix takes the map into a labyrinth full of treasure.
According to the map, the labyrinth has rooms and one-way passages, each connecting two rooms. The rooms are numbered 1 to , and room holds treasure worth . You can enter the labyrinth at room 1. With an escape kit, you can leave from any room. However, once you escape, the labyrinth collapses and you cannot go back in to look for more treasure.
Room has a hidden lever. When you go to room and pull it, a hidden two-way passage appears between rooms and .
Phoenix wants to carry out treasure whose total value is as large as possible. What is the maximum total value of treasure Phoenix can collect?
Input
The first line contains the number of rooms and the number of one-way passages , separated by a space. (, )
The second line contains integers , separated by spaces, giving the value of the treasure in each room. ()
From the third line onward, the next lines each contain two integers and , separated by a space, giving the start room and the end room of a one-way passage. (, )
For any two different rooms and , there is at most one one-way passage that starts at and ends at . (, )
Line contains the room with the lever and the two ends and of the two-way passage that appears when the lever is pulled, separated by spaces. A one-way passage from room to room , or from room to room , may already exist. (, )
Output
Print the maximum total value of treasure that can be collected in one expedition.