#P6464. [传智杯 #2 决赛] 传送门

[传智杯 #2 决赛] 传送门

Description

In Chuanzhi Academy, there are nn teaching buildings and mm bidirectional roads connecting these buildings. There are no multiple edges or self-loops. Each road has a certain length, and every pair of buildings can be reached directly or indirectly through the roads. We can easily compute the shortest paths between these buildings.

To make transportation smoother, the school decides to add a pair of portals in two teaching buildings. The portals can directly reduce the distance between this pair of buildings to 00. With the portals, the shortest path distances between some pairs of buildings become shorter.

Due to a limited budget, the school can only install one pair of portals. However, the principal wants to make it as convenient as possible for students, so the total sum of the shortest path lengths between any two points is minimized. Of course, the distance from building xx to building yy and the distance from building yy to building xx only need to be counted once.

Input Format

The first line contains two positive integers n,m(n100,m12n(n1))n,m(n\le 100,m\le\frac{1}{2}n(n-1)), representing the number of teaching buildings and roads.

The next mm lines each contain three positive integers xi,yi,wi(0<wi104)x_i,y_i,w_i(0 <w_i \le 10^4), indicating that there is a road of length wiw_i between teaching buildings xix_i and yiy_i.

Output Format

Output one line: under the optimal plan, the sum of the shortest path lengths over all unordered pairs of points.

4 5
1 2 3
1 3 6
2 3 4
2 4 7
3 4 2

14

Hint

The sample is shown in the figure. When a pair of portals is built between buildings 11 and 44, the shortest path from 11 to 22 is 33, from 11 to 33 is 0+20+2, from 11 to 44 is 00, from 22 to 33 is 44, from 22 to 44 is 3+03+0, and from 33 to 44 is 22. The sum of shortest paths is 1414, which is the best plan.

Translated by ChatGPT 5