• [POJ2377]Bad Cowtractors(最大生成树,Kruskal)


    题目链接:http://poj.org/problem?id=2377

    于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%lld", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef map<string, int> msi;
     65 typedef vector<int> vi;
     66 typedef vector<LL> vl;
     67 typedef vector<vl> vvl;
     68 typedef vector<bool> vb;
     69 
     70 typedef struct Edge {
     71     int u, v, w;
     72     Edge() {}
     73     Edge(int uu, int vv, int ww) : u(uu), v(vv), w(ww) {}
     74 }Edge;
     75 const int maxn = 1010;
     76 const int maxm = 20020;
     77 int n, m;
     78 int pre[maxn];
     79 Edge edge[maxm];
     80 
     81 bool cmp(Edge a, Edge b) {
     82     RT a.w > b.w;
     83 }
     84 
     85 int find(int x) {
     86     return x == pre[x] ? x : pre[x] = find(pre[x]);
     87 }
     88 
     89 int unite(int x, int y) {
     90     int fx = find(x);
     91     int fy = find(y);
     92     if(fx != fy) {
     93         pre[fy] = fx;
     94         return 1;
     95     }
     96     return 0;
     97 }
     98 
     99 int main() {
    100     // FRead();
    101     int u, v, c;
    102     while(~Rint(n) && ~Rint(m)) {
    103         Rep(i, n+5) pre[i] = i;
    104         Rep(i, m) {
    105             Rint(u); Rint(v); Rint(c);
    106             edge[i] = Edge(u, v, c);
    107         }
    108         sort(edge, edge+m, cmp);
    109         int ret = 0;
    110         Rep(i, m) {
    111             if(unite(edge[i].u, edge[i].v)) {
    112                 ret += edge[i].w;
    113             }
    114         }
    115         int flag = 0;
    116         For(i, 1, n+1) {
    117             if(pre[i] == i) flag++;
    118             if(flag > 1) break;
    119         }
    120         if(flag > 1) printf("-1
    ");
    121         else printf("%d
    ", ret);
    122     }
    123     RT 0;
    124 }
  • 相关阅读:
    java 集合排序
    java传值和传址
    Dom4j操作xml
    JAXP操作xml
    乐观锁和悲观锁【转】
    java IO【转】
    java 可变参数
    Eclipse调试Java的10个技巧【转】
    编译JDK源代码【转】
    Access restriction: The method typeNameToClass(String) from the type ObjectHandler is not accessible due to restriction on required library
  • 原文地址:https://www.cnblogs.com/kirai/p/5545204.html
Copyright © 2020-2023  润新知