【UVA 1395】 Slim Span (苗条树)

【题意】node

  求一颗生成树,知足最大边和最小边之差最小ios

 

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 m
a1 b1 w1
.
.
.
am bm wm
Every input item in a dataset is a non-negative integer. Items in a line are separated by a space.
n is the number of the vertices and m the number of the edges. You can assume 2 ≤ n ≤ 100 and
0 ≤ m ≤ n(n − 1)/2. ak and bk (k = 1, . . . , m) are positive integers less than or equal to n, which
represent the two vertices vak
and vbk
connected by the k-th edge ek. wk is a positive integer less than
or equal to 10000, which indicates the weight of ek. You can assume that the graph G = (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 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
50less

 

 

 

【分析】ide

  作完这道题你就变苗条了的意思。。oop

  [沉迷打机,日渐消瘦。spa

  正题->_->先把边排序,枚举最小边,而后就是让最长边最短了咯,就是最小瓶颈生成树,kruskal作最小生成树就行了。code

 

  复杂度:m^2orm

 

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 using namespace std;
 8 #define INF 0xfffffff
 9 #define Maxn 110
10 
11 struct node
12 {
13     int x,y,c;
14 }t[Maxn*Maxn];
15 int n,m;
16 
17 bool cmp(node x,node y) {return x.c<y.c;}
18 int mymax(int x,int y) {return x>y?x:y;}
19 int mymin(int x,int y) {return x<y?x:y;}
20 
21 int fa[Maxn];
22 int ffa(int x)
23 {
24     if(fa[x]!=x) fa[x]=ffa(fa[x]);
25     return fa[x];
26 }
27 
28 int main()
29 {
30     while(1)
31     {
32         scanf("%d%d",&n,&m);
33         if(n==0&&m==0) break;
34         for(int i=1;i<=m;i++) scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].c);
35         sort(t+1,t+1+m,cmp);
36         int cnt=0,ans=INF;
37         for(int i=1;i<=m;i++)
38         {
39             int cnt=0;
40             for(int j=1;j<=n;j++) fa[j]=j;
41             for(int j=i;j<=m;j++)
42             {
43                 if(ffa(t[j].x)!=ffa(t[j].y))
44                 {
45                     fa[ffa(t[j].x)]=ffa(t[j].y);
46                     cnt++;
47                 }
48                 if(cnt==n-1) {ans=mymin(ans,t[j].c-t[i].c);break;}
49             }
50         }
51         if(ans==INF) printf("-1\n");
52         else printf("%d\n",ans);
53     }
54     return 0; 
55 }
View Code

 

跟LA3887不是同样的么,LA有毒啊狂wa。。蓝书的陈锋的代码也是wa的啊smg!!blog

 

2016-11-01 21:24:53排序

相关文章
相关标签/搜索