P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper相关的知识,希望对你有一定的参考价值。

题目描述

给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)

输入格式:

 

  • Line 1: N and W separated by a space.

  • Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.

 

输出格式:

 

  • Line 1: A single integer, R, indicating the minimum number of elevator rides needed.

  • Lines 2..1+R: Each line describes the set of cows taking

one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.

状压Dp

见代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int inf=0x3f3f3f3f;
 6 int n,v,w[18],end;
 7 int dp[20][1<<20];
 8 int main() {
 9     /*
10         此处状态为前i个电梯,(bit)j的最小重量
11         dp[i][j]=>dp[i][j|(1<<k)]||dp[i+1][j|(1<<k)] ,!(k&(1<<k))
12     */
13     scanf("%d%d",&n,&v),end=1<<n;
14     for(int i=0;i<n;i++) scanf("%d",&w[i]);
15     for(int i=0;i<n;i++) for(int j=0;j<end;j++) dp[i][j]=inf;
16     for(int i=0;i<n;i++) dp[1][1<<i]=w[i];
17     for(int i=0;i<=n;i++) for(int j=0;j<end;j++) if(dp[i][j]!=inf)//存在 
18     for(int k=0;k<n;k++) if(!(j&(1<<k)))//不存在 
19     if(dp[i][j]+w[k]<=v) dp[i][j|(1<<k)]=min(dp[i][j|(1<<k)],dp[i][j]+w[k]);
20     else dp[i+1][j|(1<<k)]=min(dp[i][j|(1<<k)],w[k]);
21     for(int i=0;i<=n;i++) if(dp[i][end-1]!=inf) {
22         printf("%d\n",i);
23         break;
24     }
25     return 0;
26 }

 

以上是关于P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper的主要内容,如果未能解决你的问题,请参考以下文章

P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper [模拟退火]

P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

题解Luogu P3052 USACO12摩天大楼里的奶牛Cows in a Skyscraper

[luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)

bzoj 1597: [Usaco2008 Mar]土地购买 2011-12-27