City Hall
Time limit2sMemory limit1024 MB
Given a weighted graph where edge cost is the squared altitude difference, find the minimum path energy from S to T after raising or lowering one vertex's altitude freely.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Dynamic programming, Math
- Solved
- No attempts yet
Problem
You are the mayor of ICPC City. The city has intersections, numbered from 1 to . Intersection has altitude . Your house is at intersection , and the city hall is at intersection .
There are two-way roads, numbered from 1 to . Road directly connects intersections and . Each pair of intersections is directly connected by at most one road. Every intersection can be reached from every other intersection by traversing one or more roads.
Every morning you cycle from your house to the city hall. When you traverse a road connecting intersections and , the energy you spend is . The total energy of a path is the sum of the energy spent on each road in that path.
As mayor, you may change the altitude of at most one intersection to any non-negative real number. Using this, you want to minimize the total energy needed to cycle from your house to the city hall.
Input
The first line contains four integers , , , (; ; ; ). The next line contains integers (), the altitude of each intersection. Each of the next lines contains two integers and (), the intersections joined by road . Each pair of intersections is joined by at most one road, and the roads connect all intersections.
Output
Output one real number on a single line: the minimum total energy. An answer is accepted if its absolute error is at most .