#P9504. 『MGOI』Simple Round I | C. 魔法禁林
『MGOI』Simple Round I | C. 魔法禁林
Background
The meaning of battle is to survive. In this highly competitive world, only by becoming stronger can you survive. — Hall Magician S.
Problem Description
On the first day of school, Little M cannot wait to plan a trip to the mysterious Forbidden Forest.
Little M has two important attributes: mana and health. Very specially, at the beginning, these two values can be chosen arbitrarily by Little M.
The Forbidden Forest can be seen as an undirected simple connected graph with vertices and edges. Little M will walk in the forest, from the start vertex to .
Each time he traverses an edge, Little M’s mana decreases by . At the same time, there is a magical beast with an attack attribute on each edge, and Little M must fight it. If Little M’s mana before traversing this edge is , and the beast’s attack on this edge is , then the battle that happens while traversing this edge will consume health. The beast will not be defeated, so if the same edge is traversed multiple times, a battle will happen every time.
Little M must ensure that when his mana is fully used up, his health is , and at that moment he has arrived at vertex .
You need to find the minimum initial health Little M needs.
Input Format
The first line contains four integers .
The next lines each contain three integers , indicating that there is an edge between vertices , and the beast’s attack on this edge is .
Output Format
Output one integer in one line, representing the minimum initial health Little M needs.
3 3 1 3
1 2 2
1 3 5
3 2 3
4
5 10 1 5
2 1 3
3 1 7
4 2 4
5 3 9
5 1 7
2 3 2
5 4 6
1 4 10
5 2 5
3 4 10
6
Hint
[Sample 1 Explanation]
At the beginning, Little M chooses mana and health .
- : mana left , health left .
- : mana left , health left .
It can be proven that is the minimum initial health Little M needs.
[Constraints]
This problem uses bundled Subtask testdata.
For all testdata, , , , , the graph is an undirected simple connected graph, and .
| Subtask | Score | |||
|---|---|---|---|---|
Translated by ChatGPT 5