Test instructions: Given the graph of n nodes, the spanning tree with the lowest weight of the minimum edge is calculated for the maximum edge weight value.
Analysis: This is similar to the minimum spanning tree, from small to large enumeration of the left endpoint, for each left endpoint, and then enumerate the right endpoint, constantly update the minimum value. It's a very simple question.
#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int maxn = + 5;co NST int INF = 0x3f3f3f3f;int p[maxn];struct node{int u, V, W; BOOL operator < (const node& p) Const {return W < P.W; }};node a[10000];int Find (int x) {return x = = P[x]? x:p[x] = Find (P[x]);} int main () {int n, m; while (scanf ("%d%d", &n, &m)) {if (!m &&!n) break; for (int i = 0; i < m; ++i) scanf ("%d%d%d", &a[i].u, &A[I].V, &A[I].W); Sort (A, a+m); int ans = INF; for (int l = 0; l < m; ++l) {//enum left endpoint int cnt = n; for (int i = 0; I <= N; ++i) p[i] = i; for (int r = l; r < m; ++r) {//enum right endpoint int x = Find (A[R].U); int y = Find (A[R].V); if (x! = y) {p[x] = y; --cnt; if (1 = = cnt) {ans = min (ans, A[R].W-A[L].W);//update minimum value break; }}}} printf ("%d\n", INF = = ans -1:ans); } return 0;}
The code is as follows:
UVa 1395 Slim Span (minimum spanning tree)