Escape Room
Time limit2sMemory limit1024 MB
Given N rooms with per-room exit costs and M candidate warps with costs, choose warps and exits so every room reaches the outside, minimizing the total installation time.
- Level
Medium6 of 10
- Topics
- Minimum spanning tree, Graph, Greedy, Union-find
- Solved
- No attempts yet
Problem
Wonbin went to an escape room cafe with his friends. The cafe has rooms numbered through , and one friend is inside each room. Every room is completely isolated from the outside.
Wonbin felt bad for the friends who cannot get out, so he wants to install warps and emergency exits so that all of them can escape to the outside. He can install at most warps. Installing the -th warp takes time, and once installed it lets a person move between room and room . Each room can also have an emergency exit that connects directly to the outside, and installing an emergency exit in room takes time.
Unfortunately, Wonbin is not the brightest, so he cannot work on multiple installations of warps or emergency exits at the same time. In other words, he can start the next task only after the current one finishes.
Help Wonbin find the minimum time needed to install warps and emergency exits so that all of his friends can escape to the outside.
Input
The first line gives the number of rooms and the number of warps that can be installed . (, )
The next lines give three integers , , separated by spaces, describing a warp: installing a warp between room and room takes time. Multiple warps may connect the same pair of rooms. (, , )
The last line gives integers , ..., , where is the time needed to install an emergency exit in room . ()
Output
Print the minimum time needed to install warps and emergency exits so that all of the friends can escape to the outside.