nyoj 布线问题 (最小生成树)

。。。注定只会做水题了。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<map>
 9 #include<iomanip>
10 #include<climits>
11 #include<string.h>
12 #include<cmath>
13 #include<stdlib.h>
14 #include<vector>
15 #include<stack>
16 #include<set>
17 using namespace std;
18 #define INF 1000000007
19 #define MAXN 4010
20 #define Mod 1000007
21 #define N 100010
22 #define NN 30
23 #define sigma_size 3
24 const int MAX = 1000100;
25 const int maxn = 6e5 + 10;
26 using namespace std;
27 typedef long long LL;
28 
29 int value[555];
30 int fa[555];
31 struct node{
32     int u, v, c;
33     bool operator <(const node a) const{
34         return c < a.c;
35     }
36 }a[255*500];
37 
38 
39 int T;
40 int n, m;
41 int ans;
42 
43 int findset(int x)
44 {
45     if (fa[x] < 0) return x;
46     return fa[x] = findset(fa[x]);
47 }
48 int main()
49 {
50     cin >> T;
51     while (T--) {
52         ans = 0;
53         memset(a, 0, sizeof(a));
54         memset(value, 0, sizeof(value));
55         memset(fa,-1,sizeof(fa));
56         cin >> n >> m;
57         for (int i = 0; i < m; ++i) {
58             scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].c);
59         }
60         for (int i = 1; i <= n; ++i)
61             scanf("%d",&value[i]);
62         sort(a, a + m);
63         sort(value + 1, value + n + 1);
64         for (int i = 0; i < m; ++i) {
65             int x = findset(a[i].u);
66             int y = findset(a[i].v);
67             if (x != y) {
68                 ans += a[i].c;
69                 fa[x] += fa[y];
70                 fa[y] = x;
71             }
72         }
73         printf("%d\n", ans+value[1]);
74     }
75     //system("pause");
76     return 0;
77 }

 

posted @ 2015-03-12 11:36  UsedRose  阅读(107)  评论(0编辑  收藏  举报