zoukankan      html  css  js  c++  java
  • hoj2798 Globulous Gumdrops

    Globulous Gumdrops

    My Tags   (Edit)
      Source : 2008 Stanford Programming Contest
      Time limit : 1 sec   Memory limit : 64 M

    Submitted : 174, Accepted : 94

    Description

    Gwen just bought a bag of gumdrops! However, she does not like carrying gumdrops in plastic bags; instead, she wants to pack her gumdrops in a cylindrical tube of diameter d. Given that each of her gumdrops are perfect spheres of radii r1, r2, . . . , rn, find the shortest length tube Gwen can use to store her gumdrops.

    You should assume that the gumdrop radii are sufficiently large that no three gumdrops can be simultaneously in contact with each other while fitting in the tube. Given this restriction, it may be helpful to realize that the gumdrops will always be packed in such a way that their centers lie on a single two-dimensional plane containing the axis of rotation of the tube.

    Input

    The input file will contain multiple test cases. Each test case will consist of two lines. The first line of each test case contains an integer n (1 <= n <= 15) indicating the number of gumdrops Gloria has, and a floating point value d (2.0 <= d <= 1000.0) indicating the diameter of the cylindrical tube, separated by a space. The second line of each test case contains a sequence of n space-separated floating point numbers, r1 r2 . . . rn (1.0 <= ri <= d/2) are the radii of the gum drops in Gloria's bag. A blank line separates input test cases. A single line with the numbers "0 0" marks the end of input; do not process this case.

    Output

    For each input test case, print the length of the shortest tube, rounded to the nearest integer.

    Sample Input

    2 98.1789
    42.8602 28.7622
    3 747.702
    339.687 191.953 330.811
    0 0

    Sample Output

    138
    1628
    /*
        可以用dp[state][i]表示所用球状态为state,且最上面一个是第i个球,要放入这些球所需要的最大高度,那么每次加入一个球就可以转移了,状态转移方程为dp[state1][j]=min(dp[state1 ][j],jisuan(dp[state][i],i,j) );state1=state|( 1<<(j-1) );
    */
    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    double r[16],d,dp[1<<15][16];
    int n;
    double count(double pre,int now,int top){
            double x=r[top]+r[now];
            double y=d-r[top]-r[now];
            return pre+r[top]-r[now]+sqrt(x*x-y*y);
    }
    int main(){
            while(scanf("%d%lf",&n,&d)!=EOF){
                    if(n==0&&d==0)return 0;
                    for(int i=1;i<=n;i++)scanf("%lf",&r[i]);
                    //memset(dp,127/3,sizeof(dp));
                    for(int i=1;i<(1<<n);i++){
                            for(int j=1;j<=n;j++){
                                    dp[i][j]=0x7fffffff;
                            }
                    }
                    for(int i=1;i<=n;i++)dp[1<<(i-1)][i]=r[i]*2;
                    for(int state=1;state<(1<<n);state++){
                            for(int i=1;i<=n;i++){
                                    for(int j=1;j<=n;j++){
                                            if(state&(1<<(j-1)))continue;
                                            int state1=state|(1<<(j-1));
                                            //cout<<count(dp[state][i],i,j)<<endl;
                                            dp[state1][j]=min(dp[state1][j],count(dp[state][i],i,j));
                                    }
                            }
                    }
                    double ans=0x7fffffff;
                    for(int i=1;i<=n;i++){
                            ans=min(ans,dp[(1<<n)-1][i]);
                    }
                    int Ans=(int)(ans+0.5);
                    printf("%d
    ",Ans);
            }
    }
  • 相关阅读:
    20145330 第0周《信息安全系统设计基础》
    20145330《Java程序设计》课程总结
    20145330《Java程序设计》第五次实验报告
    20145330第十周《Java学习笔记》
    20145330第九周《Java学习笔记》
    20145330《Java程序设计》第四次实验报告
    20145330Java程序设计第三次实验
    20145330第八周《Java学习笔记》
    20145330第七周《Java学习笔记》
    20145329 《信息安全系统设计基础》第七周学习总结
  • 原文地址:https://www.cnblogs.com/thmyl/p/7341142.html
Copyright © 2011-2022 走看看