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 {v1, v2,..., vn}<tex2html_verbatim_mark> and E<tex2html_verbatim_mark> is a set of undirected edges {e1, e2,..., em}<tex2html_verbatim_mark> . Each edge eE<tex2html_verbatim_mark> has its weight w(e)<tex2html_verbatim_mark> .
A spanning tree T<tex2html_verbatim_mark> is a tree (a connected subgraph without cycles) which connects 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> .
For example, a graph G<tex2html_verbatim_mark> in Figure 5(a) has four vertices {v1, v2, v3, v4}<tex2html_verbatim_mark> and five undirected edges {e1, e2, e3, e4, e5}<tex2html_verbatim_mark> . The weights of the edges are w(e1) = 3<tex2html_verbatim_mark> , w(e2) = 5<tex2html_verbatim_mark> , w(e3) = 6<tex2html_verbatim_mark> , w(e4) = 6<tex2html_verbatim_mark> , w(e5) = 7<tex2html_verbatim_mark> as shown in Figure 5(b).
There are several spanning trees for G<tex2html_verbatim_mark> . Four of them are depicted in Figure 6(a)∼(d). The spanning tree Ta<tex2html_verbatim_mark> 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 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) 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 tree Td<tex2html_verbatim_mark> 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.
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 are separated by a space.
n<tex2html_verbatim_mark> is the number of the vertices and m<tex2html_verbatim_mark> the number of the edges. You can assume 2n100<tex2html_verbatim_mark> and 0mn(n - 1)/2<tex2html_verbatim_mark> . ak<tex2html_verbatim_mark> and bk<tex2html_verbatim_mark>(k = 1,..., m)<tex2html_verbatim_mark>are positive integers less than or equal to n<tex2html_verbatim_mark> , which represent the two vertices vak<tex2html_verbatim_mark> and vbk<tex2html_verbatim_mark> connected by the k<tex2html_verbatim_mark> -th edge ek<tex2html_verbatim_mark> . wk<tex2html_verbatim_mark> is a positive integer less than or equal to 10000, which indicates the weight of ek<tex2html_verbatim_mark> . You can assume that the graph G = (V, E)<tex2html_verbatim_mark> 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 should be printed. Otherwise, `-1' should be printed. An output should not contain extra characters.
Sample Input
4 5 1 2 3 1 3 5 1 4 6 2 4 6 3 4 7 4 6 1 2 10 1 3 100 1 4 90 2 3 20 2 4 80 3 4 40 2 1 1 2 1 3 0 3 1 1 2 1 3 3 1 2 2 2 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 1686 50
抽象:在数组中找出n-1个数,让这n-1个数的最大值和最小值的差最小,先排序然后从第一个位置开始列举即可,到 m - n+1结束
最小生成树用克鲁斯卡尔做的时候建议pre里从0开始,我是从1开始的,wa了两次,这是因为我枚举边的时候多枚举了一次,导致出现了0,会出现随机数导致的误差出现
#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++){ //建树并且得出最小的值,如果建树的过程中存在到达最后一条边了,则结束循环 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 ",minn == 0x3f3f3f? -1:minn); } }