Birthday Cake (DFS)

Source: Internet
Author: User

Test instructions

Description  

July 17 is MR.W's birthday, Acm-thu for this to make a volume of nπ m-layer birthday cake, each layer is a cylinder.
set from bottom to top number I (1 <= i <= M) layer cake is a cylinder with a radius of RI and a height of hi.  When I < M, Ri > Ri+1 and Hi > Hi+1 are required.
in order to save money as much as possible on the cake, we want the area Q to be the smallest of the outer surface of the cake (except for the bottom surface of the bottom layer).
Make q = sπ
Please program the given N and M to find out the cake making scheme (the appropriate RI and HI values) to make s minimum. (all the above data are positive integers except Q)
Input 
There are two lines, the first action N (n <= 10000), which indicates the volume of the cake to be made is nπ, and the second behavior m (M <= 20), which indicates that the layer number of the cake is M.
Output 
only one row, which is a positive integer s (s = 0 if no solution is available).
Sample Input 
-
2
Sample Output 

68

Ideas:

Because of the depth must (m), so use depth first search, top-down set cake serial number, the topmost layer for the 1th layer,......, the bottom of the cake for the first layer, it is clear that the first layer of the problem conditions (from the top layer (that is, the layer numbered 1) the minimum area Mins[i] and volume minv[i ] is in the radius of the layer and the height of the I was obtained, if the use of the general Divine search will definitely time out, so the problem also needs pruning, pruning conditions have (from the M layer up search, assuming that the level layer of the volume of V, the area of S, the current minimum area is best):
1> Because the volume of the front level layer is V, if the remaining layers of the volume of the smallest possible value, the total volume is still greater than N, then the previous level layer of the scheme is not feasible, so can be pruned (pruning conditionsis: v+minv[dep-1]>n)
2> Because the area of the front level layer is s, if the remaining layers of the area are the smallest possible value, the resulting area and than the obtained minimum area of the best, can also be pruning (pruning conditionsis: s+mins[dep-1]>best)
3> Because the volume of the front level layer is V, the volume of the remaining m-level layer satisfies: n-v= (H[k](r[k]^2) +......+h[m](r[m]^2)) (K=level+1,......, m)
And the rest of the surface area to meet: lefts=2* (R[k]h[k]+......+r[m]*h[m]) >2(N-V)/r[level] (k=level+1,......, m)
Obviously there are the above inequalities lefts=best-s>2* (N-V)/r[level]. So the pruning condition is: (n-v)/r[level]+s>best)

#include <stdio.h> #include <math.h> #define CMP (A, B) (a<b?a:b) const int Inf=0x7fffffff;int minv[21], Mins[21];int v,m;int ans;void dfs (int v,int s,int level,int r,int h)//level is the search depth, which is searched up from the underlying M-layer, r,h the radius and height of the layer {if (level==0) {        When the search is complete, update the minimum area value if (V==v&&s<ans) ans=s;    Return } if (v+minv[level-1]>v| | s+mins[level-1]>ans| |    (v-v)/r+s>=ans)//pruning 1:return;    int I,j,h_now; for (i=r-1;i>=level;i--)//enumerates each possible value of the level layer cake radius in descending order, where the minimum radius of the level layer is levels {if (level==m)//The bottom area as the initial value of the outer area (total upper area,        Only after calculating the side area) S=i*i; H_NOW=CMP ((V-v-minv[level-1])/(I*i), h-1);            Maximum height, which is the upper limit of level layer cake height, (n-v-minv[level-1]) indicates the maximum volume of the level layer for (j=h_now;j>=level;j--)//The same, the level of the lowest level    DFS (V+I*I*J,S+2*I*J,LEVEL-1,I,J);//Recursive Search sub-state}}int main () {int i;    minv[0]=0;    mins[0]=0; for (i=1;i<=20;i++)//From the top level to calculate the minimum volume and surface area of the possible value of {//from the top layer (i.e. the first layer) to the minimum volume of layer I minv[i] the first  The radius and height of the layer are I      minv[i]=minv[i-1]+i*i*i;//is not the most down volume of each layer, but the smallest volume of the i~1 layer.        Mins[i]=mins[i-1]+2*i*i;//ditto} while (scanf ("%d%d", &v,&m) ==2) {ans=inf;        The maximum radius of the +1;//m layer of the int r=sqrt (v/m).        int h=v/(m*m) The maximum height of the +1;//m layer.        DFS (0,0,M,R+1,H+1);        if (ANS==0X7FFFFFFF) printf ("0\n");    else printf ("%d\n", ans); } return 0;}

Birthday Cake (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.