#P16273. [蓝桥杯 2026 省 Java B 组] 回程

    ID: 18293 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>图论最短路2026蓝桥杯省赛

[蓝桥杯 2026 省 Java B 组] 回程

Problem Description

Given a graph with nn nodes and mm undirected edges, the nodes are numbered 1∼n1 \sim n. Each edge connects two different nodes, and multiple edges may exist. Each undirected edge is described by three integers u,v,wu, v, w, meaning there is an undirected edge between node uu and node vv with cost ww.

You need to start from node nn and reach node 11.

There is a special node XX in the graph. When you arrive at node XX for the first time, you can obtain 3 special chances. If X=nX = n, 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 ww.
  • If you use it, the cost to traverse this edge becomes 11.

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 nn to node 11. If node 11 cannot be reached, output −1-1.

Input Format

The first line contains three integers n,m,Xn, m, X, representing the number of nodes, the number of edges, and the position of the special node.

The next mm lines each contain three integers u,v,wu, v, w, representing an undirected edge connecting node uu and node vv with cost ww.

Output Format

Output one integer, the minimum total cost from node nn to node 11. If it is impossible to reach, output −1-1.

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 X=6X = 6 and the starting node is also 66, you already have 3 special chances at the beginning.

One optimal route is: 6→5→4→3→16 \to 5 \to 4 \to 3 \to 1, and the original edge costs are 20,10,100,220, 10, 100, 2, respectively.

Use special chances on 3 of these edges, for example on the edges with costs 20,10,10020, 10, 100. Then the costs of these three traversals all become 11, and the last edge still costs 22.

So the total cost is 1+1+1+2=51 + 1 + 1 + 2 = 5.

Constraints

For 30%30\% of the testdata, n,m≤500n, m \leq 500.

For another 30%30\% of the testdata, all edge costs ww are the same.

For all testdata, 1≤n≤2×1051 \leq n \leq 2 \times 10^5, 1≤m≤2×1051 \leq m \leq 2 \times 10^5, 1≤w≤1091 \leq w \leq 10^9, 1≤X,u,v≤n1 \leq X, u, v \leq n.

It is guaranteed that the graph has no self-loops, but multiple edges may exist.

Translated by ChatGPT 5