This page is still under construction.

Parts of this page are still being built. What you see may change.

All Roads Lead to Rome

Time limit1sMemory limit128 MB

Summary
Given a connected weighted graph, choose two hub nodes and assign every node to a hub to minimize the total routed distance over all ordered pairs.
Level

Medium7 of 10

Topics
Graph, Shortest path, Greedy, Brute force
Solved
No attempts yet

Problem

A city is represented as a set of locations (nodes) connected by road segments (edges). A friend of mine drives around town in an unusual way that, he claims, cuts down the number of routes he has to memorize. First he picks two distinct locations to serve as hubs, H1H_1 and H2H_2. He then assigns every other location to either H1H_1 or H2H_2, and memorizes only the shortest path from each location to its assigned hub, plus the shortest path between the two hubs. (Each hub is considered assigned to itself.)

When he travels from location AA to location BB, the length of his route is defined as follows. Let h(v)h(v) be the hub that location vv is assigned to, and let sp(x,y)\mathrm{sp}(x, y) be the shortest distance between xx and yy in the road network.

  • If h(A)=h(B)h(A) = h(B): sp(A,h(A))+sp(h(A),B)\mathrm{sp}(A, h(A)) + \mathrm{sp}(h(A), B)
  • If h(A)≠h(B)h(A) \ne h(B): sp(A,h(A))+sp(h(A),h(B))+sp(h(B),B)\mathrm{sp}(A, h(A)) + \mathrm{sp}(h(A), h(B)) + \mathrm{sp}(h(B), B)

In other words, he always visits his own hub first; if the destination's hub is different, he travels to that hub and then to the destination.

You may choose the two hubs and the assignment of the remaining locations freely. Minimize the total route length summed over every ordered pair of distinct locations (A,B)(A, B). (Because the number of locations is fixed, minimizing this total is the same as minimizing the average trip distance.) Output that minimum total.

Input

The first line of input contains the number of test cases TT.

Each test case begins with a line containing two integers nn and mm (2≤n≤502 \le n \le 50, 1≤m≤10001 \le m \le 1000), where nn is the number of locations and mm is the number of road segments directly connecting two locations. There may be more than one road segment between a pair of locations, and a road segment may start and end at the same location.

Each of the next mm lines contains three integers aa, bb, and dd (1≤a≤n1 \le a \le n, 1≤b≤n1 \le b \le n, 1≤d≤10001 \le d \le 1000), meaning the road segment between locations aa and bb has length dd. Every road is bidirectional.

A path along the road segments always exists between any two locations.

Output

For each test case, output a single line with one integer: the minimum possible total route length summed over every ordered pair of distinct locations (A,B)(A, B), minimized over all choices of the two hubs and all assignments of the remaining locations to a hub.

Examples2

  1. Example 1

    Input
    3
    3 2
    1 2 40
    2 3 20
    7 10
    1 1 1
    1 2 2
    2 4 2
    4 3 2
    3 1 2
    2 3 5
    3 7 10
    7 6 1
    5 6 1
    4 5 1
    16 15
    1 8 1
    2 8 1
    3 8 1
    4 9 1
    5 9 1
    6 9 1
    7 8 1
    8 9 3
    9 10 1
    8 11 1
    8 12 1
    8 13 1
    9 14 1
    9 15 1
    9 16 1
    
    Expected output
    240
    156
    804
    
  2. Example 2

    Input
    1
    2 1
    1 2 5
    
    Expected output
    10