POJ 3522 Slim Span
Given an undirected weighted graphG, You shoshould find one of spanning trees specified as follows.
The graphGIs an ordered pair (V,E), WhereVIs a set of vertices {V1,V2 ,...,VN} andEIs a set of undirected edges {E1,E2 ,...,EM}. Each edgeEEHas its weightW(E).
A spanning treeTIs a tree (a connected subgraph without cycles) which connects allNVerticesN-1 edges. The slimness of a spanning treeTIs defined as the difference between the largest weight and the smallest weight amongN-1 edgesT.
For example, a graphGIn Figure 5 (a) has four vertices {V1,V2,V3,V4} and five undirected edges {E1,E2,E3,E4,E5}. The weights of the edges areW(E1) = 3,W(E2) = 5,W(E3) = 6,W(E4) = 6,W(E5) = 7 as shown in Figure 5 (B ).
= 6in
There are several spanning treesG. Four of them are depicted in Figure 6 (a) values (d). The spanning treeTA in Figure 6 (a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight is 3 so that the slimness of the treeTA is 4. The slimnesses of spanning treesTB,TC andTD shown in Figure 6 (B), (c) and (d) are 3, 2 and 1, respectively. you can easily see the slimness of any other spanning tree is greater than or equal to 1, thus the spanning treeTD in Figure 6 (d) is one of the slimmest spanning trees whose slimness is 1.
Your job is to write a program that computes the smallest slimness.
Input
The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format.
NM
A1B1W1
AMBMWM
Every input item in a dataset is a non-negative integer. Items in a line are separated by a space.
NIs the number of the vertices andMThe number of the edges. You can assume 2N100 and 0MN(N-1)/2.AK andBK (K= 1 ,...,M) Are positive integers less than or equalN, Which represent the two verticesVAk andVBk connected byK-Th edgeEK.WK is a positive integer less than or equal to 10000, which indicates the weightEK. You can assume that the graphG= (V,E) Is simple, that is, there are no self-loops (that connect the same vertex) nor parallel edges (that are two or more edges whose both ends are the same two vertices ).
Output
For each dataset, if the graph has spanning trees, the smallest slimness among them shoshould be printed. Otherwise ,'-1'Could be printed. An output shoshould not contain extra characters.
Sample Input
4 5 1 2 31 3 51 4 62 4 63 4 74 6 1 2 10 1 3 100 1 4 90 2 3 20 2 4 80 3 4 40 2 1 1 2 13 0 3 1 1 2 13 3 1 2 22 3 5 1 3 6 5 10 1 2 110 1 3 120 1 4 130 1 5 120 2 3 110 2 4 120 2 5 130 3 4 120 3 5 110 4 5 120 5 10 1 2 9384 1 3 887 1 4 2778 1 5 6916 2 3 7794 2 4 8336 2 5 5387 3 4 493 3 5 6650 4 5 1422 5 8 1 2 1 2 3 100 3 4 100 4 5 100 1 5 50 2 5 50 3 5 50 4 1 150 0 0
Sample Output
1 20 0-1-1 1 0 168650
You need to find the maximum side of all spanning trees minus the minimum value of the smallest side.
Idea: You can use the kruskal idea. Connect to a tree each time, and then find the maximum edge of the tree minus the minimum edge.
AC code:
#include
#includeusing namespace std;#define maxn 105const int maxnb = maxn*(maxn-1)/2;int n,m;int f[maxn];struct p{ int x,y,w;}num[maxnb];int find(int x){ if(x!=f[x]) f[x]=find(f[x]); return f[x];}bool cmp(p a,p b){ return a.w
ans) sum=ans; } printf("%d\n",sum); } return 0;}