当前位置: 代码迷 >> 综合 >> HDU5135Little Zu Chongzhi's Triangles(状态压缩DP)
  详细解决方案

HDU5135Little Zu Chongzhi's Triangles(状态压缩DP)

热度:22   发布时间:2023-12-06 19:39:03.0

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135


题意:

给出n根木棍,随意组成几个三角形,问组合的面积最大是多少;


思路:
用状态压缩,首先把可以组成的三角形的合法情况全都放进vector里,然后枚举。


#include<bits/stdc++.h>
using namespace std;
int s[12];
double dp[1<<12];
vector<int> v;
double cul(int a, int b, int c)
{if(a + b <= c)return 0.0;double p = (a + b + c) * 0.5;return sqrt(p*(p-a)*(p-b)*(p-c));
}
int main()
{int n;while(scanf("%d", &n) != EOF && n){v.clear();memset(dp, 0, sizeof(dp));for(int i = 0; i < n; i++)scanf("%d", &s[i]);sort(s, s+n);for(int i = 0; i < n; i++)for(int j = i+1; j < n; j++)for(int k = j+1; k < n; k++){int st = (1<<i) | (1<<j) | (1<<k);dp[st] = cul(s[i], s[j], s[k]);if(s[i] + s[j] > s[k])v.push_back(st);}for(int i = 0; i < (1 << n); i++)for(int j = 0; j < v.size(); j++){if(i & v[j])continue;dp[i|v[j]] = max(dp[i|v[j]], dp[i] + dp[v[j]]);}printf("%.2f\n", dp[(1<<n)-1]);}return 0;
}