UVa 1354 Mobile computing[Brute Force enumeration]

Source: Internet
Author: User

 **1354 Mobile Computing**

There is a mysterious planet called Yaen, whose space is 2-dimensional. There is many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make friendly mobile arts of them to decorate their 2-dimensional living rooms.
In their 2-dimensional world, a mobile is defined recursively as follows:
a stone hung by a string, or
a rod of length 1 with a sub-mobiles at both ends; The rod is hung by a string at the center of gravity of Sub-mobiles. When the weights of the Sub-mobiles is N and M, and their distances from the center of gravity is a and b respectively, The equation Nxa = MXB holds.
For example, if you got three stones with weights 1, 1, and 2, here is some possible mobiles and their widths:
Given the weights of stones and the width of the the, your task is to design the widest possible mobile satisfying both o f the following conditions.
• It uses all the stones.
its width is less than the width of the the.
You should ignore the widths of stones.
In some cases, sub-mobiles hung from both ends of a rod might overlap (see the figure on the right). Such mobiles is acceptable. The width of the example is (1/3) + 1 + (1/4).
Input
The first line of the input gives the number of datasets. Then the specified number of datasets follow. A DataSet has the following format.
R S W1.
Ws
R is a decimal fraction representing the width of the the class, which satisfies 0 < R < 10. S is the number of the stones. May assume 1≤s≤6. WI is the weight of the i-th stone, which are an integer. May assume 1≤wi≤1000.
Input
The first line of the input gives the number of datasets. Then the specified number of datasets follow. A DataSet has the following format.
R S W1.
Ws
R is a decimal fraction representing the width of the the class, which satisfies 0 < R < 10. S is the number of the stones. May assume 1≤s≤6. WI is the weight of the i-th stone, which are an integer. May assume 1≤wi≤1000.
You can assume this no mobiles whose widths is between r−0.00001 and R + 0.00001 can be made of given stones.
Output
For each dataset in the input, one line containing a decimal fraction should is output. The decimal fraction should give the width of the widest possible mobile as defined above. An output line should not contain extra characters such as spaces.
In case there is no mobile which satisfies the requirement, answer '-1 ' instead.
The answer should not has an error greater than 0.00000001. You may output any numb er of digits after the decimal point, provided that the AB Ove accuracy condition are satisfied.
Sample Input
5
1.3
3
1
2
1
1.4
3
1
2
1
2.0
3
1
2
1
1.59
4
2
1
1
3
1.7143
4
1
2
3
5
Sample Output
-1
1.3333333333333335
1.6666666666666667
1.5833333333333335
1.7142857142857142

Problem Solving Ideas:
1. Use the bottom-up method to enumerate the tree-randomly select two subtrees trees each time to merge into a tree, each node sequentially numbered.
2. For a certain tree, its length must be determined. With the root node as the origin of the axis, DFS calculates the width of each node relative to its root node.
Note : The input is only one stone, output 0;

1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 Const intmaxn= -;7 intLCHILD[MAXN];//left child number8 intRCHILD[MAXN];//Right Child number9 intWIGHT[MAXN];//quality of the number correspondingTen intVIS[MAXN];//-1 indicates no number exists 0 indicates number is not in tree 1 indicates in tree One DoubleDIS[MAXN]; A  - DoubleR,ans; - ints; the voidinit () { -ans=0; -memset (Lchild,-1,sizeoflchild); -memset (Rchild,-1,sizeofrchild); +Memset (Wight,0,sizeofWight); -memset (Vis,-1,sizeofvis); + } A  at voidCalculateintID) {//calculates the distance from each number relative to the root node. -     if(lchild[id]!=-1){ -dis[lchild[id]]=dis[id]-Double(Wight[rchild[id]])/Double(wight[lchild[id]]+Wight[rchild[id]]); -dis[rchild[id]]=dis[id]+Double(Wight[lchild[id]])/Double(wight[lchild[id]]+Wight[rchild[id]]); - calculate (Lchild[id]); - calculate (Rchild[id]); in     } - } to  + voidSearchintCntintm) {//m for this stage stone max number -     if(cnt==1){ thememset (DIS,0,sizeofdis); *Calculate0); $         DoubleA=0, b=0;Panax Notoginseng          for(intI=0; i<maxn;i++){ -             if(dis[i]<a) a=Dis[i]; the             if(dis[i]>b) b=Dis[i]; +         } A         Doublec=b-A; the      //cout<< "" <<c<<endl; +         if(C<r&&c>ans) ans=C; -         return ; $     } $      for(intI=1; i<maxn;i++){ -         if(vis[i]==0){ -vis[i]=1; the              for(intj=1; j<maxn;j++){ -                 if(vis[j]==0){Wuyivis[j]=1; the                     if(cnt==2){ -  Wulchild[0]=i;rchild[0]=J; -wight[0]=wight[i]+Wight[j]; AboutSearch (cnt-1, m); $                     } -                     Else{ -  -vis[m+1]=0; Alchild[m+1]=i;rchild[m+1]=J; +wight[m+1]=wight[i]+Wight[j]; theSearch (cnt-1, m+1); -vis[m+1]=-1; $                     } thevis[j]=0; the                 } the             } thevis[i]=0; -         } in     } the } the intMain () { About     //freopen ("Input.txt", "RB", stdin); the     //freopen ("Output.txt", "WB", stdout); the     intN; thescanf"%d",&N); +      while(n--){ - init (); thescanf"%lf%d",&r,&s);Bayi  the          for(intI=1; i<=s;i++){ thescanf"%d",&wight[i]); -vis[i]=0; -         } the         if(s==1) {printf ("%.16f\n", ans);Continue;} the search (s,s); the         if(ans==0) cout<<"-1"<<Endl; the         Elseprintf"%.16f\n", ans); -     } the     return 0; the}

UVa 1354 Mobile computing[Brute Force enumeration]

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.