• ZOJ 3204 Connect them MST-Kruscal


    这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的

    因为是裸的 Kruscal ,所以就直接上代码了~

    Source Code : 

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <string>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    #define MOD 1000000007
    #define pi acos(-1.0)
    
    using namespace std;
    
    typedef long long           ll      ;
    typedef unsigned long long  ull     ;
    typedef unsigned int        uint    ;
    typedef unsigned char       uchar   ;
    
    template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
    template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}
    
    const double eps = 1e-8         ;
    const int N = 210               ;
    const int M = 1100011*2         ;
    const ll P = 10000000097ll      ;
    const double MINN = -999999999.9;
    const int INF = 0x3f3f3f3f      ;
    const int MAXN = 110            ;
    
    int root[MAXN];
    struct Edge {
        int from,to;
        int w;
    } edge[MAXN * MAXN];
    
    int tol;
    Edge ans[MAXN * MAXN];
    int cnt;
    
    void addedge (int u, int v, int w) {
        edge[tol].from = u;
        edge[tol].to = v;
        edge[tol].w = w;
        ++tol;
    }
    
    bool cmp1 (Edge a, Edge b) {
        if(a.w != b.w)    return a.w < b.w;
        else if (a.from != b.from)  return a.from < b.from;
        else return a.to < b.to;
    }
    
    bool cmp2 (Edge a, Edge b) {
        if (a.from != b.from)   return a.from < b.from;
        else return a.to < b.to;
    }
    
    int find (int x) {
        if (root[x] == -1)  return x;
        return root[x] = find (root[x]);
    }
    
    void kruscal () {
        memset (root,-1,sizeof(root));
        cnt = 0;    //加入最小生成树的边
        for (int k = 0; k < tol; ++k) {
            int u = edge[k].from;
            int v = edge[k].to;
            int t1 = find(u);
            int t2 = find(v);
            if(t1 != t2) {
                ans[cnt++] = edge[k];
                root[t1] = t2;
            }
        }
    }
    
    int main () {
        int T;
        scanf("%d",&T);
        int n;
        while (T--) {
            scanf ("%d",&n);
            tol = 0;
            int w;
            for (int i = 1; i <= n; ++i) {
              for (int j = 1; j <= n; ++j) {
                  scanf("%d",&w);
                  if(j <= i || w == 0)    continue;
                  addedge(i, j, w);
              }
            }
            sort (edge, edge + tol, cmp1);
            kruscal ();
            if (cnt != n - 1) {
                printf("-1
    ");
                continue;
            } else {
                sort (ans, ans + cnt, cmp2);
                for (int i = 0; i < cnt - 1; ++i) {
                  printf("%d %d ", ans[i].from, ans[i].to);
                }
                printf("%d %d
    ", ans[cnt - 1].from, ans[cnt - 1].to);
            }
        }
        return 0;
    }
  • 相关阅读:
    ElasticSearch : APT-GET安装方式
    python__基础 : 多继承中方法的调用顺序 __mro__方法
    TypeScript : 语法及特性
    Spring : JDBC模板, 事务和测试
    数据库 : 事物以及隔离性导致的问题
    MySQL数据库 : 函数和存储过程
    Java : java基础(6) 反射与枚举
    Java : java基础(5) Socket网络编程
    Java : java基础(4) 线程
    Java : java基础(3) IO流
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4387409.html
Copyright © 2020-2023  润新知