zoukankan      html  css  js  c++  java
  • [SCOI2005]繁忙的都市

    嘟嘟嘟

    裸的最小生成树。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<cstdlib>
     7 #include<vector>
     8 #include<queue>
     9 #include<stack>
    10 #include<cctype>
    11 using namespace std;
    12 #define enter puts("")
    13 #define space putchar(' ')
    14 #define Mem(a) memset(a, 0, sizeof(a))
    15 typedef long long ll;
    16 typedef double db;
    17 const int INF = 0x3f3f3f3f;
    18 const db eps = 1e-8;
    19 const int maxn = 5e4 + 5;
    20 inline ll read()
    21 {
    22     ll ans = 0;
    23     char ch = getchar(), last = ' ';
    24     while(!isdigit(ch)) {last = ch; ch = getchar();}
    25     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
    26     if(last == '-') ans = -ans;
    27     return ans;
    28 }
    29 inline void write(ll x)
    30 {
    31     if(x < 0) putchar('-'), x = -x;
    32     if(x >= 10) write(x / 10);
    33     putchar(x % 10 + '0');
    34 }
    35 
    36 int n, m;
    37 struct Node
    38 {
    39     int x, y, w;
    40     bool operator < (const Node& other)const
    41     {
    42         return w < other.w;
    43     }
    44 }t[maxn];
    45 
    46 int Max = 0, cnt = 0;
    47 
    48 int p[maxn];
    49 void init()
    50 {
    51     for(int i = 1; i <= n; ++i) p[i] = i;
    52 }
    53 int Find(int x)
    54 {
    55     return x == p[x] ? x : p[x] = Find(p[x]);
    56 }
    57 
    58 int main()
    59 {
    60     n = read(); m = read();
    61     init();
    62     for(int i = 1; i <= m; ++i) {t[i].x = read(); t[i].y = read(); t[i].w = read();}
    63     sort(t + 1, t + m + 1);
    64     for(int i = 1; i <= m; ++i)
    65     {
    66         int px = Find(t[i].x), py = Find(t[i].y);
    67         if(px != py) 
    68         {
    69             p[px] = py;
    70             cnt++; Max = t[i].w;
    71             if(cnt == n - 1) break;
    72         }
    73     }
    74     write(n - 1); space; write(Max); enter;
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    文本文件、二进制文件
    trunc()
    字符集、编码
    windows注册表:扫盲
    decode() & sign()
    移动前端工作的那些事前端制作之自适应制作篇
    css hack知识详解
    IE6兼容性大全
    JS正则匹配入门基础!
    [转载]Javascript中批量定义CSS样式 cssText属性
  • 原文地址:https://www.cnblogs.com/mrclr/p/9496396.html
Copyright © 2011-2022 走看看