zoukankan      html  css  js  c++  java
  • kuangbin_MST C (POJ 2031)

    全程double精度就能过了 间接0距离不用管 prim自动连起来的

    G++交的话只能用%f输出 C++的话加不加l都可以 (这么说以后用%f肯定不会错咯)

    不过我不懂为什么他们的空间时间差了好多倍...

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define INF 0x3F3F3F3F
    using namespace std;
    
    struct Node{double x, y, z, r;}node[110];
    
    typedef pair<double, int> pdi;
    struct cmp{
        bool operator () (const pdi a, const pdi b){
            return a.first > b.first;
        }
    };
    
    double val[110][110];
    
    double nodeDist(Node a, Node b){
        return max(0.0, sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
                         + (a.z-b.z)*(a.z-b.z)) - a.r - b.r);
    }
    
    double prim(int n, int s)
    {
        double ans = 0, dist[110];
        bool vis[110];
        memset(vis, false, sizeof vis);
        priority_queue<pdi, vector<pdi>, cmp> q;
        
        for(int i = 1; i <= n; i++){
            dist[i] = val[s][i];
            q.push(make_pair(dist[i], i));
        }
        vis[s] = true;
        while(!q.empty()){
            pdi u = q.top();
            q.pop();
            if(vis[u.second]) continue;
            vis[u.second] = true;
            ans += u.first;
            for(int i = 1; i <= n; i++){
                if(!vis[i] && dist[i] > val[u.second][i]){
                    dist[i] = val[u.second][i];
                    q.push(make_pair(dist[i], i));
                }
            }
        }
        return ans;
    }
    
    int main()
    {
        int n;
        while(scanf("%d", &n), n){
            for(int i = 1; i <= n; i++){
                scanf("%lf%lf%lf%lf", &node[i].x, &node[i].y, &node[i].z, &node[i].r);
                for(int j = 1; j <= i; j++){
                    val[i][j] = val[j][i] = nodeDist(node[i], node[j]);
                }
            }
            printf("%.3f
    ", prim(n, 1));
        }
        return 0;
    }
  • 相关阅读:
    Linux 头文件详解
    Linux 进程运行状态
    配置uboot指定nfs挂载根文件系统
    (实例)Linux 内核添加exfat驱动
    Linux 内核 编译模块
    简单添加自己的驱动程序到Linux内核树中
    Linux 生成随机mac地址,并固化到本地
    (转)为什么ssh一关闭,程序就不再运行了?
    Ubuntu 安装 QtCreator (version : Qt 5.9.8)
    Ubuntu 固定自己的IP
  • 原文地址:https://www.cnblogs.com/quasar/p/5144537.html
Copyright © 2011-2022 走看看