zoukankan      html  css  js  c++  java
  • POJ2031 Building a Space Station 最小生成树

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

      很典型的最小生成树题目。

     1 //STATUS:C++_AC_0MS_252KB
     2 #include<stdio.h>
     3 #include<stdlib.h>
     4 #include<string.h>
     5 #include<math.h>
     6 #include<iostream>
     7 #include<string>
     8 #include<algorithm>
     9 #include<vector>
    10 #include<queue>
    11 #include<stack>
    12 using namespace std;
    13 #define LL __int64
    14 #define pii pair<int,int>
    15 #define Max(a,b) ((a)>(b)?(a):(b))
    16 #define Min(a,b) ((a)<(b)?(a):(b))
    17 #define mem(a,b) memset(a,b,sizeof(a))
    18 #define lson l,mid,rt<<1
    19 #define rson mid+1,r,rt<<1|1
    20 const int N=110,M=1000000,INF=0x3f3f3f3f,MOD=1999997;
    21 const LL LLNF=0x3f3f3f3f3f3f3f3fLL;
    22 const double DNF=100000000;
    23 
    24 struct Node{
    25     double x,y,z,r;
    26 }nod[N];
    27 double w[N][N],d[N];
    28 int vis[N];
    29 int n;
    30 
    31 inline double dist(Node &a,Node &b)
    32 {
    33     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)
    34                 +(a.z-b.z)*(a.z-b.z));
    35 }
    36 
    37 double mintree()
    38 {
    39     int i,j,x;
    40     double m,ans=0;
    41     for(i=0;i<n;i++)d[i]=DNF;
    42     d[0]=0;
    43     mem(vis,0);
    44     for(i=0;i<n;i++){
    45         m=DNF;
    46         for(j=0;j<n;j++)if(d[j]<m && !vis[j])m=d[x=j];
    47         vis[x]=1;
    48         ans+=d[x];
    49         for(j=0;j<n;j++){
    50             if(w[x][j]<d[j] && !vis[j])
    51                 d[j]=w[x][j];
    52         }
    53     }
    54     return ans;
    55 }
    56 
    57 int main()
    58 {
    59  //   freopen("in.txt","r",stdin);
    60     int i,j;
    61     double t;
    62     while(~scanf("%d",&n) && n)
    63     {
    64         for(i=0;i<n;i++)
    65             for(j=0;j<n;j++)w[i][j]=DNF;
    66         for(i=0;i<n;i++)
    67             scanf("%lf%lf%lf%lf",&nod[i].x,&nod[i].y,&nod[i].z,&nod[i].r);
    68         for(i=0;i<n;i++){
    69             for(j=i;j<n;j++){
    70                 t=dist(nod[i],nod[j])-nod[i].r-nod[j].r;
    71                 w[i][j]=w[j][i]=t>0?t:0;
    72             }
    73         }
    74 
    75         printf("%.3lf\n",mintree());
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    Codeforces Round #296 (Div. 2B. Error Correct System
    实验十二 图的建立与遍历
    1561: (More) Multiplication
    1562: Fun House
    hdu 2203 亲和串
    hdu 3549Flow Problem
    poj 2182 Lost Cows
    poj 3468A Simple Problem with Integers
    hdu1698 Just a Hook
    栈和队列的面试题Java实现
  • 原文地址:https://www.cnblogs.com/zhsl/p/2868248.html
Copyright © 2011-2022 走看看