zoukankan      html  css  js  c++  java
  • HDU

    题目链接

    思路:把每个顶点编号,点之间的距离>=10且<=100,给这个两个点之间建边。建完之后求求最小生成树(MST)

    AC代码

    #include <cmath>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int N = 110;
    struct node{
        int u, v;
        double w;
    }q[N*N];
    int n, m, cnt;
    int xx[N], yy[N], pre[N];
    void init(){
        for(int i = 1; i <= n; i++){
            pre[i] = i;
        }
        cnt = 0;
    }
    int Find(int x){
        if(x != pre[x]) pre[x] = Find(pre[x]);
        return pre[x];
    }
    void join(int x, int y){
        int tx = Find(x);
        int ty = Find(y);
        if(tx != ty) pre[ty] = tx;
    }
    bool cmp(node a, node b){
        return a.w < b.w;
    }
    double Kru(){
        double ans = 0;
        sort(q, q + cnt, cmp);
        for(int  i = 0; i < cnt; i++){
            if(Find(q[i].u) != Find(q[i].v)){
                join(q[i].u, q[i].v);
                ans += q[i].w;
            }
        }
        return ans;
    }
    int main(){
        #ifdef ONLINE_JUDGE
        #else
            freopen("in.txt", "r", stdin);
        #endif //ONLINE_JUDGE
        int t;
        scanf("%d", &t);
        while(t--){
            scanf("%d", &n);
            init();
            for(int  i = 1; i <= n; i++){
                scanf("%d%d", &xx[i], &yy[i]);
            }
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= n; j++){
    
                    int dis = (xx[i] - xx[j])*(xx[i] - xx[j])+(yy[i] - yy[j])*(yy[i] - yy[j]);
                    if(dis >= 100.0 && dis <= 1000000.0) {
                        q[cnt].u = i;
                        q[cnt].v = j;
                        q[cnt++].w = sqrt((double)dis);
                    }
                }
            }
            double ans = Kru();
            int cnt1 = 0;
            for(int i = 2; i <= n; i++){
                if(pre[i] == i) cnt1++;
            }
            if(cnt1 > 1) printf("oh!
    ");
            else  printf("%.1lf
    ", ans*100.0);
        }
        return 0;
    }
    
  • 相关阅读:
    ubuntu安装后做得几件事情 【robby_chan】
    malloc函数的一种简单的原理性实现[转]
    了解B树 B+树
    win下格式转为utf8 编码 转码
    log4j2与slf4j日志桥接
    java获取当前行数
    java获取服务器ip地址解决linux上为127.0.0.1的问题
    log4j2的基本使用
    navicator使用之mysql
    log4j与log4j2日志文件的操作
  • 原文地址:https://www.cnblogs.com/kun-/p/9838488.html
Copyright © 2011-2022 走看看