zoukankan      html  css  js  c++  java
  • HDU1875 畅通工程再续

    题目链接

    代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    const double INF = (1<<25);
    
    #define MAXN 103
    #define MAXM 10010
    
    struct Pos{
        int x, y;
    }pos[MAXN];
    
    
    int vis[MAXN];
    double G[MAXN][MAXN], d[MAXN], ans;
    
    
    double calc(struct Pos a, struct Pos b){
        int k = (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
        if(k<100 || k>1000000) return INF;
        else return sqrt(k*1.0);
    }
    
    
    int prim(int n){
        int i, x, y, flag;
        double m;
    
    
        for(i=0; i<n; i++){
            d[i] = G[0][i];
        }
        vis[0] = 1;
        d[0] = 0;
    
    
        for(i=0; i<n-1; i++){
            m = INF*1.0;
            flag = 0;
            for(y=0; y<n; y++)if(!vis[y] && m > d[y]) {m = d[x=y]; flag = 1;}
            if(!flag) break;
            vis[x] = 1;
            ans += m;
            for(y=0; y<n; y++)if(!vis[y] && d[y]>G[x][y]) d[y] = G[x][y];
        }
        return flag;
    }
    
    
    int main(){
        int n, i, j, T;
        scanf("%d", &T);
        while(T--){
            scanf("%d", &n);
            ans = 0.0;
    
    
            for(i=0; i<n; i++){
                scanf("%d %d", &pos[i].x, &pos[i].y);
                vis[i] = 0;
            }
    
            for(i=0; i<n; i++){
                for(j=0; j<=i; j++){
                    if(j==i) G[i][j] = INF;
                    else G[i][j] = G[j][i] = calc(pos[i], pos[j]);
                }
            }
    
            if(prim(n)) printf("%.1lf\n", ans*100);
            else printf("oh!\n");
        }
    
        return 0;
    }
  • 相关阅读:
    centos中文乱码修改字符编码使用centos支持中文
    java知识总结-26
    java知识总结-25
    java知识总结-24
    java知识总结-23
    java知识总结-22
    java知识总结-21
    java知识总结-20
    java知识总结-19
    java知识总结-18
  • 原文地址:https://www.cnblogs.com/tanhehe/p/2939337.html
Copyright © 2011-2022 走看看