#P16273. [蓝桥杯 2026 省 Java B 组] 回程
[蓝桥杯 2026 省 Java B 组] 回程
Problem Description
Given a graph with nodes and undirected edges, the nodes are numbered . Each edge connects two different nodes, and multiple edges may exist. Each undirected edge is described by three integers , meaning there is an undirected edge between node and node with cost .
You need to start from node and reach node .
There is a special node in the graph. When you arrive at node for the first time, you can obtain 3 special chances. If , it means you already have these 3 special chances at the start.
During the subsequent trip, each time you traverse an edge, you may choose whether to use one special chance:
- If you do not use it, the cost to traverse this edge is its original cost .
- If you use it, the cost to traverse this edge becomes .
You may use fewer or none of the special chances, but in total you can use at most 3 times, and each use only applies to the single edge you are traversing at that moment.
Now, compute the minimum total cost from node to node . If node cannot be reached, output .
Input Format
The first line contains three integers , representing the number of nodes, the number of edges, and the position of the special node.
The next lines each contain three integers , representing an undirected edge connecting node and node with cost .
Output Format
Output one integer, the minimum total cost from node to node . If it is impossible to reach, output .
6 6 6
1 2 5
2 4 5
1 3 2
3 4 100
4 5 10
5 6 20
5
Hint
Sample Explanation
Since and the starting node is also , you already have 3 special chances at the beginning.
One optimal route is: , and the original edge costs are , respectively.
Use special chances on 3 of these edges, for example on the edges with costs . Then the costs of these three traversals all become , and the last edge still costs .
So the total cost is .
Constraints
For of the testdata, .
For another of the testdata, all edge costs are the same.
For all testdata, , , , .
It is guaranteed that the graph has no self-loops, but multiple edges may exist.
Translated by ChatGPT 5