hdu5135 状态压缩dp||2014广州站I题

http://acm.hdu.edu.cn/showproblem.php?pid=5135

Problem Description
Zu Chongzhi (429–500) was a prominent Chinese mathematician and astronomer during the Liu Song and Southern Qi Dynasties. Zu calculated the value ofπ to the precision of six decimal places and for a thousand years thereafter no subsequent mathematician computed a value this precise. Zu calculated one year as 365.24281481 days, which is very close to 365.24219878 days as we know today. He also worked on deducing the formula for the volume of a sphere. 

It is said in some legend story books that when Zu was a little boy, he liked mathematical games. One day, his father gave him some wood sticks as toys. Zu Chongzhi found a interesting problem using them. He wanted to make some triangles by those sticks, and he wanted the total area of all triangles he made to be as large as possible. The rules were :

1) A triangle could only consist of 3 sticks.
2) A triangle's vertexes must be end points of sticks. A triangle's vertex couldn't be in the middle of a stick.
3) Zu didn't have to use all sticks.

Unfortunately, Zu didn't solve that problem because it was an algorithm problem rather than a mathematical problem. You can't solve that problem without a computer if there are too many sticks. So please bring your computer and go back to Zu's time to help him so that maybe you can change the history.
 

Input
There are no more than 10 test cases. For each case:

The first line is an integer N(3 <= N<= 12), indicating the number of sticks Zu Chongzhi had got. The second line contains N integers, meaning the length of N sticks. The length of a stick is no more than 100. The input ends with N = 0.
 

Output
For each test case, output the maximum total area of triangles Zu could make. Round the result to 2 digits after decimal point. If Zu couldn't make any triangle, print 0.00 .
 

Sample Input
   
   
   
   
3 1 1 20 7 3 4 5 3 4 5 90 0
 

Sample Output
   
   
   
   
0.00 13.64
 

/**
题意:从给定的边中挑选组成三角形,求所组成三角形面积和最大为多少
思路:状态压缩dp[i]表示i状态下对应的最大面积,i对应二进制位为1则对应位的边长已选,0则未选,状态转移方程见代码
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

int a[20];
double dp[(1<<12)+10];
int n;

int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        int t=(1<<n)-1;
        double maxx=0.0;
        memset(dp,0,sizeof(dp));
        for(int i=0; i<=t; i++)
        {
            for(int x=0; x<n-2; x++)
            {
                for(int y=x+1; y<n-1; y++)
                {
                    for(int z=y+1; z<n; z++)
                    {
                        if(a[x]+a[y]>a[z]&&a[z]+a[x]>a[y]&&a[z]+a[y]>a[x])
                        {
                            if((i&(1<<x))==0&&(i&(1<<y))==0&&(i&(1<<z))==0)
                            {
                                //printf("**\n");
                                double p = (a[x]+a[y]+a[z]) * 1.0 / 2;
                                double temp = sqrt(p * (p - a[x]) * (p - a[y]) * (p - a[z]));
                                dp[i|(1<<x)|(1<<y)|(1<<z)]=max(dp[i]+temp,dp[i|(1<<x)|(1<<y)|(1<<z)]);
                                maxx=max(maxx,dp[i|(1<<x)|(1<<y)|(1<<z)]);
                            }
                        }
                    }
                }
            }
        }
        printf("%.2lf\n",maxx);
    }
    return 0;
}


你可能感兴趣的:(hdu5135 状态压缩dp||2014广州站I题)