• 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
    ", ans+value[1]);
    74     }
    75     //system("pause");
    76     return 0;
    77 }
  • 相关阅读:
    js 随笔
    在windows下安装Composer(转载)
    博客添加背景音乐
    安装mysql5.7报错启动失败(3534)的处理
    windows 下搭php环境
    使用composer创建项目时报错:Composer could not find the config file?
    mysql-存储过程(二)-逻辑判断语句
    mysql-存储过程(1) mysql循环语句
    php批量更新mysql中的数据
    支付宝 APP登录 获取用户信息 PHP(转)
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4331953.html
Copyright © 2020-2023  润新知