B-slim SpanTime
limit:3000MS
Memory Limit:0KB
64bit IO Format:%lld &%llu SubmitStatus
Description
Given an undirected weighted graph G<tex2html_verbatim_mark>, you should find one of spanning trees Specified as follows.
The Graph G <tex2html_verbatim_mark> is an ordered Pair V , E ) <tex2html_verbatim_mark> Where v <tex2html_verbatim_mark> is A set of Vertices { V 1, V 2,..., v n}<tex2html_verbatim_mark> and E < Tex2html_verbatim_mark> is a set of undirected edges { e 1, e 2 ,..., e m}<tex2html_verbatim_mark> . Each edge e e <tex2html_verbatim_mark> has its weight < Span class= "MATH" > w ( e ) <tex2html_verbatim_mark>
A spanning tree T<tex2html_verbatim_mark> is a tree (a connected subgraph without cycles) which connect s all the n<tex2html_verbatim_mark> vertices with n -1<tex2html_verbatim_mark> Edges. The slimness of a spanning tree T<tex2html_verbatim_mark> is defined as the difference between the Largest weight and the smallest weight among the n -1<tex2html_verbatim_mark> edges of t <tex2html_verbatim_mark>.
<tex2html_verbatim_mark>
For example, a graph G <tex2html_verbatim_mark> in Figure 5 (a) have four Vertices { v 1, v 2, v 3, v 4}< Tex2html_verbatim_mark> and five undirected edges { e 1, e 2, e 3, e 4, e 5}<tex2html_verbatim_mark> . The weights of the edges Are w ( e 1) = 3<tex2html_verbatim_mark> w ( e 2) = 5<tex2html_verbatim_mark> , w ( e 3) = 6<tex2html_verbatim_mark> , W ( e 4) = 6<tex2html_verbatim_mark> , w ( e 5) = 7 <tex2html_verbatim_mark> as shown in Figure 5 (b).
=6in <tex2html_verbatim_mark>
There is several spanning trees forG<tex2html_verbatim_mark>. Four of them is depicted in Figure 6 (a) ˆ¼ (d). The Spanning tree Ta<tex2html_verbatim_mark> in Figure 6 (a) have three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight are 3 so that the slimness of the tree TA<tex2html_verbatim _mark> is 4. The slimnesses of spanning trees Tb<tex2html_verbatim_mark>, tc<tex2html_verbatim _mark> and Td<tex2html_verbatim_mark> shown in Figure 6 (b), (c) and (d) is 3, 2 and 1, respectively . You can easily see the slimness of any other spanning tree are greater than or equal to 1, thus the spanning tree td<tex2html_verbatim_mark> in Figure 6 (d) are 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 and zeros separated by a space. Each dataset has the following format.
n<tex2html_verbatim_mark>m<tex2html_verbatim_mark>
a1<tex2html_verbatim_mark>b1<tex2html_verbatim_mark>w1< Tex2html_verbatim_mark>
<tex2html_verbatim_mark>
am<tex2html_verbatim_mark>bm<tex2html_verbatim_mark>wm <tex2html_verbatim_mark>
Every input item in a dataset is a non-negative integer. Items in a line is separated by a space.
N<tex2html_verbatim_mark> is the number of the vertices andm<tex2html_verbatim_mark> the number of the edges. You can assume2N100<tex2html_verbatim_mark> and0mN(N-1)/2<tex2html_verbatim_mark>.aK<tex2html_verbatim_mark> andbK<tex2html_verbatim_mark>(k= 1,...,m) <tex2html_verbatim_mark>are positive integers less than or equal to n <tex2html_verbatim_mark> , which represent the Vertices v ak<tex2html_verbatim_mark> and v bk<tex2html_ verbatim_mark> connected by The K <tex2html_verbatim_mark> - Th edge e k<tex2html_verbatim_mark> . W k<tex2html_verbatim_mark> is a positive integer less than or equal to 10000, which indicates the weight of e k<tex2html_verbatim_mark> . You can assume that the Graph G = ( V , E ) <tex2html_ Verbatim_mark> is simple, which is, there be no self-loops (that's connect the same vertex) nor parallel edges (that is, or more edges whose both ends is the same).
Output
For each dataset, if the graph has spanning trees, the smallest slimness among them should is printed. Otherwise, '-1' should be printed. An output should 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 1 0 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 1686 50
Abstract: In the array to find the number of n-1, so that the n-1 number of the maximum and minimum difference between the minimum, first sort and then start from the first position to enumerate, to m-n+1 end
Minimum spanning tree when using Kruskal, it is recommended that the pre starts from 0, I start from 1, WA two times, this is because I enumerate the edges of the time enumerated once, resulting in 0, there will be random number caused by errors appear
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int pre[110];struct node{int x,y,values;} Edges[10010];int CMP (Node A, Node B) {return a.values < b.values;} void Init () {for (int i = 1;i<=110;i++) {Pre[i] = i; }}int finds (int x) {while (x! = Pre[x]) {x = pre[x]; } return x;} int main () {int n,m; while (scanf ("%d%d", &n,&m), n| | m) {int a,b,c; int minn = 0x3f3f3f; for (int i = 0; i<m; i++) {scanf ("%d%d%d", &a,&b,&c); edges[i].x = A; Edges[i].y = b; Edges[i].values = C; } sort (edges,edges+m,cmp); for (int i = 0;i<= m-n +1;i++) {//build and derive the smallest value if the process of achievement reaches the last edge, then the end loop int j = i; int nums = 0; Init (); for (; j<m;j++) {int u = finds (edges[j].x); int v = finds (EDGES[J].Y); if (U = v) {Pre[u] = V; Nums + +; } if (nums = = n-1) {minn = min (minn,edges[j].values-edges[i].values); Break }} if (j = = m) break; } printf ("%d\n", Minn = = 0x3f3f3f -1:minn); }}
--Minimum spanning tree