• 【HDOJ】2988 Dark roads


    最小生成树。

     1 /*  */
     2 #include <iostream>
     3 #include <string>
     4 #include <map>
     5 #include <queue>
     6 #include <set>
     7 #include <stack>
     8 #include <vector>
     9 #include <deque>
    10 #include <algorithm>
    11 #include <cstdio>
    12 #include <cmath>
    13 #include <ctime>
    14 #include <cstring>
    15 #include <climits>
    16 #include <cctype>
    17 #include <cassert>
    18 using namespace std;
    19 
    20 #define rep(i, a, n)     for (int i=a;i<n;++i)
    21 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
    22 #define pb                 push_back
    23 #define mp                 make_pair
    24 #define all(x)             (x).begin(),(x).end()
    25 #define SZ(x)             ((int)(x).size())
    26 #define lson            l, mid, rt<<1
    27 #define rson            mid+1, r, rt<<1|1
    28 
    29 typedef struct Edge_t {
    30     int u, v, w;
    31     friend bool operator <(const Edge_t &a, const Edge_t &b) {
    32         return a.w < b.w;
    33     }
    34 } Edge_t;
    35 
    36 const int maxn = 200005;
    37 int pre[maxn];
    38 Edge_t E[maxn];
    39 
    40 int find(int x) {
    41     if (x == pre[x])
    42         return x;
    43     return pre[x] = find(pre[x]);
    44 }
    45 
    46 int main() {
    47     int i, j, k;
    48     int n, m;
    49     int ans;
    50     int fu, fv;
    51     
    52     #ifndef ONLINE_JUDGE
    53         freopen("data.in", "r", stdin);
    54         freopen("data.out", "w", stdout);
    55     #endif
    56     
    57     while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
    58         for (i=0; i<n; ++i)
    59             pre[i] = i;
    60         for (i=0; i<m; ++i)
    61             scanf("%d %d %d", &E[i].u, &E[i].v, &E[i].w);
    62         sort(E, E+m);
    63         ans = 0;
    64         for (i=0; i<m; ++i) {
    65             fu = find(E[i].u);
    66             fv = find(E[i].v);
    67             if (fu != fv)
    68                 pre[fu] = fv;
    69             else
    70                 ans += E[i].w;
    71         }
    72         printf("%d
    ", ans);
    73     }
    74     
    75     #ifndef ONLINE_JUDGE
    76         printf("time = %d.
    ", (int)clock());
    77     #endif
    78     
    79     return 0;
    80 }
  • 相关阅读:
    无废话XML--XML约束(DTD)
    无废话XML--XML细节
    XML,HTML,XHTML
    javabean内省
    反射应用--IOC和AOP
    JDK动态代理
    反射
    类加载机制
    linkin大话数据结构--泛型
    java异常处理
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4464285.html
Copyright © 2020-2023  润新知