Mission
Time limit1sMemory limit1024 MB
In an undirected weighted graph, find the shortest simple path from B to H that passes through E.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
Problem
Attention, soldier.
I have a special task for you. We have detected an enemy base and it needs to be destroyed. You will be given a map and enough bombs to blow it up. After the action a helicopter will be waiting for you in the forest nearby.
Sounds easy, doesn't it? Find the fastest way to reach the goal, and make sure you do not visit any place twice. Visit a place twice and you will be detected.
Is everything clear? Very well. Get ready, because you are leaving in 10 minutes.
I wish you good luck. Don't get killed, and see you at dinner.
You are given an undirected graph and three different vertices of it: your base, the enemy base, and the place where the helicopter is waiting. Find the shortest path in the graph from your base to the helicopter's place. The path must go through the enemy base and must not visit any vertex twice.
Input
The first line contains five space separated integers , , , , .
The graph has vertices numbered to . Your home base is at vertex , the enemy base at vertex , and the helicopter waits at vertex . It holds , and , , are pairwise different.
Each of the following lines describes one edge with three space separated integers , , (, , ). It means there is an undirected edge connecting vertices and , and traversing it costs units of time.
No two vertices are connected by more than one edge.
It holds and .
Output
Print a single line with a single integer, the least amount of time needed to complete the mission. If the mission cannot be completed, print -1 instead.