Relocation
Links: POJ - 2923
Title effect: to give you two cars, each car has a rated load c1, c2, give you n items (n <= 10) of the weight Wi, each time with two round-trip transportation vehicles, the minimum number of requirements.
Idea: to see a small number n, first think of violence, can enumerate each case of loading, to determine whether the establishment of the situation prior to loading the state of compression, 0 for not installed, 1 to wear, to state. violence then readily occur to be enumerated with a backpack 01 determines whether the program is feasible, is determined as follows: add the total weight of the selected item, then c1 maximum weight capacity of the vehicle 01 can hold the backpack request, if the car c2 weight> -c1 total weight of the car can hold true representative of the maximum weight is returned possible. Next, the state of the article is dp, the state 0 to the state (1 << n) -1, each transfer means enumerate viable car program, the state transition equation is as follows:
dp[i|sta[j]]=min(dp[i|sta[j]],dp[i]+1);
sta [j] is the program loading enumeration, i | sta [j] is the assembled state after loading.
Code:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include<iostream> 5 #include<cmath> 6 #define INF 0x3f3f3f3f 7 #define MAXN 100000 8 using namespace std; 9 typedef long long ll; 10 int f[105],dp[1<<12],w[MAXN],sta[MAXN],c1,c2,n; 11 bool check(int st) 12 { 13 memset(f,0,sizeof(f)); 14 int sum=0; 15 for(int i=0;i<n;i++) 16 { 17 if(st&(1<<i)) 18 { 19 sum+=w[i]; 20 if(sum>c1+c2)continue; 21 for(int j=c1;j>=w[i];j--) 22 f[j]=max(f[j],f[j-w[i]]+w[i]); 23 } 24 } 25 if(c2>=sum-f[c1])return true; 26 return false; 27 } 28 int main() 29 { 30 ios::sync_with_stdio(false); 31 int cas; 32 cin>>cas; 33 for(int ca=1; ca<=cas; ca++) 34 { 35 memset(dp,INF,sizeof(dp)); 36 dp[0]=0; 37 cin>>n>>c1>>c2; 38 c1 > c2 ? c1 ^= (c2 ^= (c1 ^= c2)) : 0; 39 for(int i=0;i<n;i++) 40 cin>>w[i]; 41 int cnt=0; 42 for(int i=0;i<(1<<n);i++) 43 if(check(i))sta[cnt++]=i; 44 for(int i=0;i<(1<<n);i++) 45 { 46 for(int j=0;j<cnt;j++) 47 dp[i|sta[j]]=min(dp[i|sta[j]],dp[i]+1); 48 } 49 cout<<"Scenario #"<<ca<<":"<<endl; 50 cout<<dp[(1<<n)-1]<<endl; 51 if(ca<cas)cout<<endl; 52 } 53 return 0; 54 }