Problem list
Log in
#usaco0160 · USACO 2014-2015 December Silver

Piggyback

Silver III 2.0 s 256 MB

Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back to the barn to rest. They come up with a plan to minimize the total amount of energy they both spend while walking.

Bessie spends B units of energy when walking from a field to an adjacent field, and Elsie spends E units. However, if Bessie and Elsie are together in the same field, Bessie can carry Elsie on her shoulders and both can move to an adjacent field while spending only P units of energy. If P is very small, the most efficient solution may involve them meeting at a common field, then traveling together piggyback for the rest of the journey. Of course, if P is large, it may make more sense for them to travel separately.

Given B, E, and P, as well as the layout of the farm, please compute the minimum amount of energy required for Bessie and Elsie to reach the barn.

Input

  • Line 1: The positive integers B, E, P, N, and M (all at most 40,000). N is the number of fields (numbered 1..N, N >= 3), and M is the number of connections. Bessie and Elsie start in fields 1 and 2 respectively. The barn is in field N.
  • Lines 2..1+M: Each describes a bi-directional connection between a pair of different fields, specified by their integer indices.

Output

A single integer specifying the minimum amount of energy Bessie and Elsie collectively need to spend to reach the barn.

Example 1

Input
4 4 5 8 8
1 4
2 3
3 4
4 7
2 5
5 6
6 8
7 8
Output
22

Output details: Bessie goes 1->4, Elsie goes 2->3->4, then they travel together 4->7->8.

Source
USACO 2014-2015 December Silver
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code