Tim and Ray are two students from the Department of Computer Science at NTHU. Before graduation, they plan to embark on a walking trip around Taiwan. However, as college students facing a future where AI may reduce job opportunities, they have extremely limited funds. Therefore, they must carefully plan the expenses of their entire journey.
First, they need to determine their destination. Ray lives in Tamsui and survives by eating Tamsui Agei (a traditional Taiwanese food) every day. Before graduation, he hopes to taste food other than Agei and rice noodles. Tim has “riding gongwan” (Hsinchu’s famous pork balls, humorously described as a mode of transportation). for four years throughout his university life and now wishes to learn how to use other modes of transportation.
While they were troubled by their decision,
Allen made a suggestion: “Why not go to Pingtung? Not only can you eat delicious tuna, but you can also ride at Eluanbi Lighthouse. Most importantly, you may even see the long-lost Sam.”
Ray: “Oh right! I haven’t seen Sam in a long time.”
Tim: “He still owes me a McDonald’s.”
Allen: “Does Pingtung even have McDonald’s?”
Ray: “I think they only have Dandan Burger.”
Tim: “Fine, Dandan is fine! As long as he treats us, I’m satisfied.”
Ray: “Alright then, it’s Pingtung!!!”
After some time planning, they decided to travel throughout the entire summer vacation.
Allen: “The entire summer!? Doesn’t that mean we could eventually reach anywhere even if we wander randomly?”
Tim: “Exactly. To avoid worrying about time, I already quit my summer camp job.”
Thus, the only thing they truly need to care about is money.
Ray: “Great! Now I can fully enjoy the journey of finding Sam. Tuna, here I come!”
Allen: “I just received a phone call from Sam’s father. Since he is a skilled mountaineer, he can lend us a tent!”
Ray: “Then we only need to manage our food supply!”
Tim: “I have implemented software that lists the unit price of food in different cities.”
Allen: “And I built an app that calculates how many units of food are consumed when traveling between certain cities.”
Ray: “That’s perfect! In that case, I will find a way to obtain a program that combines your information and computes how much money we need to prepare.”
Additionally, Prof. C provides K vouchers, each of which can be exchanged for one unit of food for free.
Since Ray is a good friend of Prof. C, and you are Prof. C’s students, naturally this task is handed over to you.
According to Ray’s notes, you are given a map containing N cities and M undirected roads.
At any city:
For each undirected roadi, connecting cities ui and vi, traveling through the road consumes a fixed amount of food costi:
Refill system:
The order of actions is refill -> buying or voucher transactions (order doesn't matter)
Once they first arrive at the destination city, the journey is considered completed. The remaining food amount does not matter, and no refund is provided.
Your task is to compute the minimum total amount of money required to complete the journey.
Problem Constraints: Header Usage
Standard Phrasing:
You may use any headers from the C++ Standard Library.
Sample Explanation
The optimal route is hsinchu → miaoli → hsinchu → taichung → changhua → pingtung.
At hsinchu, Ray's friend automatically refills 3 units of food for free. Tim then uses the voucher to gain 1 more unit, bringing the total to 4 units.
They then travel to miaoli (consuming 3 units, food: 4→1) and purchase 4 units of food at the local price of 50, spending 200 in total.
They backtrack to hsinchu (consuming 3 units, food: 5→2), where the refill triggers again (since 2<5), restoring 3 free units (food: 2→5).
From hsinchu, they travel directly to taichung via the shortcut (consuming 3 units, food: 5→2), then continue to changhua (consuming 2 units, food: 2→0).
Upon arriving at changhua, the refill triggers (since 0<2), providing 8 free units (food: 0→8).
Finally, they travel to pingtung (consuming 8 units, food: 8→0), completing the journey.
The total cost is 50×4=200.


by gemini
testcases
| 1-2 | N ≤ 10, K=0, R=0 |
| 3-4 | K=0, R=0 |
| 5-6 | K=0 |
| 7-8 | R=0 |
| 9-10 | No Limit |
The first line contains five integers N M C K R.
The next N lines each contain:
CityNamej Vj
The next M lines each contain three space-separated items:
Cityu Cityv costi
After the road descriptions, the next R lines each contain:
CityNamei Ti si
Upon arriving at CityNamei:
The last line contains two strings:
StartCity EndCity
Note: The graph is not guaranteed to be connected.