您的位置:首页 > 其它

POJ -- 1948 二维背包问题

2013-04-10 20:59 405 查看
Triangular Pastures
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5389 Accepted: 1709
Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite. 
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N (3 <= N <= 40) fence segments (each of integer length Li (1 <= Li <= 40) and must arrange them into a triangular pasture with the largest grazing area. Ms. Hei must use all the rails to create three sides of non-zero length. 
Help Ms. Hei convince the rest of the herd that plenty of grazing land will be available.Calculate the largest area that may be enclosed with a supplied set of fence segments. 
Input
* Line 1: A single integer N 
* Lines 2..N+1: N lines, each with a single integer representing one fence segment's length. The lengths are not necessarily unique. 
Output
A single line with the integer that is the truncated integer representation of the largest possible enclosed area multiplied by 100. Output -1 if no triangle of positive area may be constructed. 
Sample Input
5
1
1
3
3
4

Sample Output
692

思路:
二维背包,三角形中的某条边 i 看作第一维费用,另一条边看作是第二维费用 j,背包容量为总长的一半(三角形任意一条边都不可能比周长的一半大)则第三边为总长度sum-i-j;
找出所有满足的i,j,枚举求出最大面积即可。其中面积公式为{ 1.t=sum/2; 2.area=sqrt(t*(t-i)*(t-j)*(t-(sum-i-j));}
状体转移方程为   dp[ k ][ i ][ j ] = dp[ k-1 ][ i-sticks[k] ][ j ] | dp[ k-1 ][ i ][ j-sticks[k] ]; 意思是取第k条棍子的时候,组成的两条边长度分别为 i 和 j 。
如何01背包一样,可将数组降为二维。既有dp[ i ][ j ] = dp[ i-sticks[k]][ j ]  |  dp[ i ][ j-sticks[k] ],逆序循环。

View Code
1 #include <iostream>
2 #include <cmath>
3 #include <cstdio>
4 #include <cstring>
5 #include <algorithm>
6
7 using namespace std;
8
9 int sticks[45];
10 int dp[1000][1000];
11
12 double getArea(int a,int b,int c)
13 {
14     double t = (a+b+c)/2.0;
15     return sqrt(t*(t-a)*(t-b)*(t-c));
16 }
17
18 int main()
19 {
20     int N,sum;
21     scanf("%d",&N);
22     for(int i=1;i<=N;i++)
23     {
24         scanf("%d",&sticks[i]);
25         sum += sticks[i];
26     }
27     int limit=sum/2;
28     memset(dp,0,sizeof(dp));
29     dp[0][0]=1;
30     for(int k=1;k<
acac
;=N;k++)
31     {
32         for(int i=limit;i>=0;i--)
33         {
34             for(int j=limit;j>=0;j--)
35             {
36                 if(i>=sticks[k] && dp[i-sticks[k]][j])
37                     dp[i][j]=1;
38                 if(j>=sticks[k] && dp[i][j-sticks[k]])
39                     dp[i][j]=1;
40
41             }
42         }
43     }
44     double ans = 0;
45     for(int i=1;i<=limit;i++)
46     {
47         for(int j=1;j<=limit;j++)
48         {
49             if(dp[i][j])
50             {
51                 int t=sum-i-j;
52                 if(t+i>j && t+j>i && i+j>t)
53                 {
54                     double temp=getArea(i,j,t);
55                     if(temp>ans)
56                         ans = temp;
57                 }
58             }
59         }
60     }
61     if(ans == 0)
62         printf("-1\n");
63     else
64         printf("%d\n",(int)(ans*100));
65     return 0;
66 }
  

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: