poj3522

题意:找出一个图的生成树中最大边权值和最小边权值差最小的值。

题解参见:

http://blog.csdn.net/sdj222555/article/details/7698978

每次枚举最小边,然后求生成树,更新结果。

  1 //Accepted    200 KB    94 ms
2 //kruskal
3 #include <cstdio>
4 #include <cstring>
5 #include <cmath>
6 #include <algorithm>
7 using namespace std;
8
9 const int MAXN = 105;
10 const int MAXM = MAXN*MAXN;
11 const int inf=10000000;
12
13 int min(int a,int b)
14 {
15 return a>b?b:a;
16 }
17 int max(int a,int b)
18 {
19 return a>b?a:b;
20 }
21
22 struct node
23 {
24 int u,v,c;
25 node(int u,int v,int c):u(u),v(v),c(c)
26 {
27
28 }
29 node()
30 {
31
32 }
33 }p[MAXM];
34 int e;
35
36 void addnode(int u,int v,int c)
37 {
38 p[e++]=node(u,v,c);
39 }
40 int cmp(node x,node y)
41 {
42 return x.c<y.c;
43 }
44 int f[MAXN];
45 int n,m;
46 void build()
47 {
48 for (int i=0;i<=n;i++)
49 {
50 f[i]=i;
51 }
52 }
53 int find(int x)
54 {
55 if (x==f[x]) return x;
56 return f[x]=find(f[x]);
57 }
58 void union_set(int x,int y)
59 {
60 int fx=find(x),fy=find(y);
61 if (fx!=fy)
62 {
63 f[fy]=fx;
64 }
65 }
66 void kruskal()
67 {
68 int ans=inf;
69 int cnt;
70 int tmin,tmax;
71 sort(p,p+e,cmp);
72 for (int i=0;i<m;i++)
73 {
74 build();
75 cnt=0;
76 tmax=-1;
77 tmin=inf;
78 for (int j=i;j<e;j++)
79 {
80 int fx=find(p[j].u);
81 int fy=find(p[j].v);
82 if (fx!=fy)
83 {
84 union_set(fx,fy);
85 tmax=max(tmax,p[j].c);
86 tmin=min(tmin,p[i].c);
87 cnt++;
88 if (cnt==n-1) break;
89 }
90 }
91 if (cnt==n-1)
92 ans=min(ans,tmax-tmin);
93 }
94 if (ans==inf) ans=-1;
95 printf("%d\n",ans);
96 }
97 int main()
98 {
99 while (scanf("%d%d",&n,&m),n+m)
100 {
101 e=0;
102 int u,v,c;
103 for (int i=0;i<m;i++)
104 {
105 scanf("%d%d%d",&u,&v,&c);
106 addnode(u,v,c);
107 }
108 kruskal();
109 }
110 return 0;
111 }

View
Code

时间: 2024-10-24 21:51:02

poj3522的相关文章

【poj3522】 Slim Span

http://poj.org/problem?id=3522 (题目链接) 题意 求最小生成树的最大边与最小边差最小. Solution 排序后滑动窗口维护 代码 // poj3522 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #define LL long lo

POJ3522 Slim Span

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3959 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V 

POJ-3522 Slim Span(最小生成树)

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8633   Accepted: 4608 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V 

poj3522 kruskal+枚举

题目的意思是求构成生成树的边的最大边和最小边的差最小.枚举即可 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define maxn 102 struct node { int x; int y; int val; }s[maxn*(maxn-1)/2]; int n,m,pa[maxn*(maxn-1)/2]; bool cmp(node a,node b)

【kruscal】【最小生成树】poj3522 Slim Span

求一个生成树,使得最大边权和最小边权之差最小.由于数据太小,暴力枚举下界,求出相应的上界.最后取min即可. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int n,m,fa[101],rank[101]; 6 void clear(){for(int i=1;i<=n;i++) fa[i]=i; memset(rank,0,sizeof

Uva1395 POJ3522 Slim Span (最小生成树)

Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V is a set of vertices {v1, v2, -, vn} and E is a set of undirected edges {e1, e2, -, em}. Each

poj-3522 最小生成树

Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V is a set of vertices {v1, v2, -, vn} and E is a set of undirected edges {e1, e2, -, em}. Each

暑假集训day5

今天的主要内容为最小生成树.判负环和差分约束系统 苗条的最小生成树 poj3522 本题排序完枚举最小边,Kruskal跑n遍即可. #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int INF=0x7fffffff; inline int read(){ int num=0,t=1;char c=getchar(); while(c>'9'||

最小生成树练习2(Kruskal)

两个BUG鸣翠柳,一行代码上西天... hdu4786 Fibonacci Tree(生成树)问能否用白边和黑边构成一棵生成树,并且白边数量是斐波那契数. 题解:分别优先加入白边和黑边,求出生成树能包含白边的最大值和最小值,其间有值为斐波那契数即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int M=1e5+1; 6 cons