P3052 [USACO12MAR]摩天大樓裏的奶牛Cows in a Skyscraper
阿新 • • 發佈:2017-11-23
sep separate weight algorithm with != color cow std
題目描述
給出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 */ 13scanf("%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