bzoj 1083: [SCOI2005]繁忙的都市

裸最小生成树。。。一开始看是SCOI想应该没这么简单。

结果真尼玛这么简单。。好意思!!

 1 /*
 2 ID:WULALA
 3 PROB:bzoj1083 
 4 LANG:C++
 5 */
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <algorithm>
 9 #include <cmath>
10 #include <iostream>
11 #include <fstream>
12 #include <ctime>
13 #define N 308 
14 #define M 10008 
15 #define mod
16 #define mid(l,r) ((l+r) >> 1)
17 #define INF 0x7ffffff
18 using namespace std;
19 
20 int n,m,fa[N],cnt,ans,tot;
21 
22 struct WULALA
23 {
24     int x,y,value;
25 }e[2*M];
26 
27 void add(int x,int y,int w)
28 {
29     e[++cnt].value = w;
30     e[cnt].y = y;
31     e[cnt].x = x;
32 }
33 
34 void init()
35 {
36     scanf("%d%d",&n,&m);
37     for (int i = 1;i <= m;i++)
38     {
39         int a,b,c;
40         scanf("%d%d%d",&a,&b,&c);
41         add(a,b,c);
42     }
43     for(int i = 1;i <= n;i++) fa[i] = i;
44 }
45 
46 bool cmp(WULALA a,WULALA b)
47 {
48     return (a.value < b.value);
49 }
50 
51 int find(int a)
52 {
53     if (a == fa[a]) return a;
54     return (fa[a] = find(fa[a]));
55 }
56 
57 void work()
58 {
59     sort(&e[1],&e[cnt+1],cmp);
60     for (int i = 1;i <= cnt;i++)
61         if(find(e[i].x) != find(e[i].y))
62         {
63             ans = e[i].value;
64             tot++;
65             fa[find(e[i].x)] = e[i].y;
66         } 
67     printf("%d %d\n",tot,ans);
68 }
69 
70 int main()
71 {
72     init();
73     work();
74     return 0;
75 } 
View Code

 

posted @ 2014-01-06 19:16  乌拉拉979  阅读(155)  评论(0编辑  收藏  举报