In an undirected weighted graph, find the shortest simple path from B to H that passes through E.
Medium7GraphShortest pathNo attempts yetTime limit1sMemory limit1024 MBAttention, 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.
The first line contains five space separated integers N, M, B, E, H.
The graph has N vertices numbered 1 to N. Your home base is at vertex B, the enemy base at vertex E, and the helicopter waits at vertex H. It holds 1≤B,E,H≤N, and B, E, H are pairwise different.
Each of the following M lines describes one edge with three space separated integers v, w, t (1≤v,w≤N, v=w, 1≤t≤1000000). It means there is an undirected edge connecting vertices v and w, and traversing it costs t units of time.
No two vertices are connected by more than one edge.
It holds 3≤N≤1000 and 0≤M≤1000.
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.