가중치가 있는 연결 그래프에서 가장 긴 경로의 길이(지름)가 최소가 되는 신장 트리를 찾아, 그 지름과 트리의 간선들을 출력한다.
어려움9그래프최단 경로최소 신장 트리그리디아직 제출이 없습니다시간 제한5초메모리 제한1024 MBYou are given a simple connected undirected weighted graph G with N nodes and M edges. Each node is numbered 1 through N.
A spanning tree of G is a subgraph of G, which is a tree and connects all the vertices of G. The diameter of a tree is the length of the longest path among the paths between any two nodes in the tree. A minimum diameter spanning tree of G is a spanning tree of G that has a minimum diameter.
Write a program that finds any minimum diameter spanning tree.
The first line of the input contains two integers N (2≤N≤500) and M (N−1≤M≤2N(N−1)).
Then M lines follow: The i (1≤i≤M)-th line contains three space-separated integers u_i, v_i and l_i (1≤u_i,v_i≤N, 1≤l_i≤109); it describes a bidirectional edge connecting vertex u_i and vertex v_i with length l_i.
It is guaranteed that the given graph doesn't have any loops or multiple edges, and the graph is connected.
In the first line, print the diameter of the minimum diameter spanning tree of G.
In the next N−1 lines, print the description of the edges in the minimum diameter spanning tree of G. The j (1≤j≤N−1)-th line should contain two space-separated integers x_i and y_i (1≤x_i, y_i≤N); it describes a bidirectional edge connecting vertex x_i and y_i.
If there are several possible answers, print any one of them.