bzoj1083 [SCOI2005]繁忙的都市

题目链接

裸kruskal算法

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<string>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 #include<ctime>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<set>
13 using namespace std;
14 int n,m,fa[333],q,w,e,tot,o;
15 int getint()
16 {
17     int ret=0,f=1;char ch=getchar();
18     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
19     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
20     return f==-1?-ret:ret;
21 }
22 struct bian
23 {
24     int u,v,w;
25     bian(int u=0,int v=0,int w=0):u(u),v(v),w(w){}
26     bool operator < (const bian &a)const
27     {
28         return w<a.w;
29     }
30 }bi[100010];
31 int find(int x)
32 {
33     return x==fa[x]?x:fa[x]=find(fa[x]);
34 }
35 int main()
36 {
37     n=getint(),m=getint();
38     for(int i=1;i<=n;i++)fa[i]=i;
39     for(int i=1;i<=m;i++)
40     {
41         q=getint(),w=getint(),e=getint();
42         bi[i]=bian(q,w,e);
43     }
44     sort(bi+1,bi+m+1);
45     for(int i=1;i<=m;i++)
46     {
47         q=find(bi[i].u),w=find(bi[i].v);
48         if(q!=w)
49         {
50             fa[q]=w,tot++;
51             if(tot==n-1)o=i;
52         }
53     }
54     printf("%d %d",n-1,bi[o].w);
55     return 0;
56 }

 

posted @ 2016-01-22 16:18  HugeGun  阅读(167)  评论(0编辑  收藏  举报