zoukankan      html  css  js  c++  java
  • POJ 2031 Building a Space Station

    描述

    传送门:我是传送门

    You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task.
    The space station is made up with a number of units, called cells. All cells are sphere-shaped, but their sizes are not necessarily uniform. Each cell is fixed at its predetermined position shortly after the station is successfully put into its orbit. It is quite strange that two cells may be touching each other, or even may be overlapping. In an extreme case, a cell may be totally enclosing another one. I do not know how such arrangements are possible. 

    All the cells must be connected, since crew members should be able to walk from any cell to any other cell. They can walk from a cell A to another cell B, if, (1) A and B are touching each other or overlapping, (2) A and B are connected by a `corridor’, or (3) there is a cell C such that walking from A to C, and also from B to C are both possible. Note that the condition (3) should be interpreted transitively. 

    You are expected to design a configuration, namely, which pairs of cells are to be connected with corridors. There is some freedom in the corridor configuration. For example, if there are three cells A, B and C, not touching nor overlapping each other, at least three plans are possible in order to connect all three cells. The first is to build corridors A-B and A-C, the second B-C and B-A, the third C-A and C-B. The cost of building a corridor is proportional to its length. Therefore, you should choose a plan with the shortest total length of the corridors. 

    You can ignore the width of a corridor. A corridor is built between points on two cells’ surfaces. It can be made arbitrarily long, but of course the shortest one is chosen. Even if two corridors A-B and C-D intersect in space, they are not considered to form a connection path between (for example) A and C. In other words, you may consider that two corridors never intersect. 

    输入

    The input consists of multiple data sets. Each data set is given in the following format. 

    n
    x1 y1 z1 r1
    x2 y2 z2 r2

    xn yn zn rn 

    The first line of a data set contains an integer n, which is the number of cells. n is positive, and does not exceed 100. 

    The following n lines are descriptions of cells. Four values in a line are x-, y- and z-coordinates of the center, and radius (called r in the rest of the problem) of the sphere, in this order. Each value is given by a decimal fraction, with 3 digits after the decimal point. Values are separated by a space character. 

    Each of x, y, z and r is positive and is less than 100.0. 

    The end of the input is indicated by a line containing a zero. 

    输出

    For each data set, the shortest total length of the corridors should be printed, each in a separate line. The printed values should have 3 digits after the decimal point. They may not have an error greater than 0.001. 

    Note that if no corridors are necessary, that is, if all the cells are connected without corridors, the shortest total length of the corridors is 0.000. 

    样例

    输入

    3
    10.000 10.000 50.000 10.000
    40.000 10.000 50.000 10.000
    40.000 40.000 50.000 10.000
    2
    30.000 30.000 30.000 20.000
    40.000 40.000 40.000 20.000
    5
    5.729 15.143 3.996 25.837
    6.013 14.372 4.818 10.671
    80.115 63.292 84.477 15.120
    64.095 80.924 70.029 14.881
    39.472 85.116 71.369 5.553

    0

    输出

    20.000
    0.000
    73.834

    思路

    只需要注意下判断距离就好了

    代码

      1 /*
      2  * ================================================
      3  *
      4  *       Filename:  C.cpp
      5  *
      6  *           Link:  http://poj.org/problem?id=2031
      7  *
      8  *        Version:  1.0
      9  *        Created:  2018/09/26 16时16分00秒
     10  *       Revision:  none
     11  *       Compiler:  g++
     12  *
     13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
     14  *   Organization:  QLU_浪在ACM
     15  *
     16  * ================================================
     17  */
     18 #include<iostream>
     19 #include<cstdio>
     20 #include<cstring>
     21 #include<string>
     22 #include<algorithm>
     23 #include <cmath>
     24 using namespace std;
     25 #define clr(a, x) memset(a, x, sizeof(a))
     26 #define rep(i,a,n) for(int i=a;i<=n;i++)
     27 #define pre(i,a,n) for(int i=n;i>=a;i--)
     28 #define ll long long
     29 #define max3(a,b,c) fmax(a,fmax(b,c))
     30 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     31 const double eps = 1e-6;
     32 const int INF = 0x3f3f3f3f;
     33 const int mod = 1e9 + 7;
     34 const int N = 110;
     35 struct no 
     36 {
     37     double x,y,z,r;
     38 }d[N];
     39 
     40 int n,m,now,tot;
     41 double ans,mmin;
     42 double cost[N][N];
     43 double v2[N];
     44 bool v1[N];
     45 double judge(int i,int j)
     46 {
     47     double t1,t2,t3;
     48     t1 = (d[i].x-d[j].x);   t1 = t1*t1;
     49     t2 = (d[i].y-d[j].y);   t2 = t2*t2;
     50     t3 = (d[i].z-d[j].z);   t3 = t3*t3;
     51     t1 = t1+t2+t3;
     52     t1 = sqrt(t1);
     53     t1 = t1-(d[i].r+d[j].r);
     54     if(t1-0.0 < eps)
     55         return 0.0;
     56     else 
     57         return t1;
     58 }
     59 
     60 void init()
     61 {
     62     clr(v1,0);
     63     rep(i,1,n)
     64         rep(j,i+1,n)
     65             cost[i][j] = cost[j][i] = INF;
     66 }
     67 
     68 double prim()
     69 {
     70     v1[1] = 1;
     71     ans = 0.0;
     72     tot = 1;
     73     for(int i = 1;i <= n;i++)
     74         v2[i] = cost[1][i];
     75     while(tot < n)
     76     {
     77         mmin = INF;
     78         tot++;
     79         for(int i = 1;i <= n;i++)
     80         {
     81             if(!v1[i] && mmin-v2[i] > eps)
     82             {
     83                 mmin = v2[i];
     84                 now = i;
     85             }
     86         }
     87         ans += mmin;
     88         for(int i = 1;i <= n;i++)
     89         {
     90             if(!v1[i] && v2[i]-cost[now][i] > eps)
     91                 v2[i] = cost[now][i];
     92         }
     93         v1[now] = 1;
     94     }
     95     return ans;
     96 }
     97 
     98 int main()
     99 {
    100     ios
    101 #ifdef ONLINE_JUDGE 
    102 #else 
    103         freopen("in.txt","r",stdin);
    104     // freopen("out.txt","w",stdout); 
    105 #endif
    106     while(scanf("%d",&n) != EOF)
    107     {
    108         if(n == 0)
    109             break;
    110         init();
    111         rep(i,1,n)
    112         {
    113             scanf("%lf %lf %lf %lf",&d[i].x,&d[i].y,&d[i].z,&d[i].r);
    114         }
    115         rep(i,1,n)
    116         {
    117             rep(j,i+1,n)
    118             {
    119                 double tmp = judge(i,j);
    120                 cost[i][j] = cost[j][i] = tmp;
    121             }
    122         }
    123         printf("%.3f
    ",prim());
    124     }
    125     fclose(stdin);
    126     // fclose(stdout);
    127     return 0;
    128 }
  • 相关阅读:
    get_json_object 用法
    vim中的特殊字符
    vim常用命令
    mac下如何配置用户的环境变量
    vim如何替换^M ?
    git ssh 配置
    mac上pip install时提示/System/Library/... 无权限
    mysql语句--table中某列有多值时,删除其他,仅保留1条
    mysql语句如何把多行的数据合并到一行?
    微服务分布式电商项目学习笔记(二)---- 微服务架构图+微服务划分图(2020/7/1)
  • 原文地址:https://www.cnblogs.com/duny31030/p/14305100.html
Copyright © 2011-2022 走看看