码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-23 08:18:44      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:print   names   needed   ber   rate   follow   描述   single   状压   

题目描述

给出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

标签:print   names   needed   ber   rate   follow   描述   single   状压   

原文地址:http://www.cnblogs.com/InfoEoR/p/7881504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!