P3052 [USACO12MAR] Cows in a Skyscraper G ( 状压dp

#include
using namespace std;
using VI = vector;
using ll = long long;
using PII = pair ;
int n,m;
int w[21];
int dp[1<<20];
int g[1<<20];
int main(){
    cin>>n>>m;
    for(int i = 0 ; i < n ; i++){
        cin>>w[i];
    }
    memset(dp , 0x3f , sizeof dp);
    dp[0] = 1;
    g[0] = m;
    for(int i = 0 ; i < (1 << n) - 1; i++){
        for(int j = 0 ; j < n ; j++){
            if(1 << j & i) continue;
            else{
                int st = (1 << j) | i;
                if(g[i] >= w[j] && dp[st] >= dp[i]){
                    if(dp[st] == dp[i]) g[st] = max(g[i] - w[j] , g[st]);
                    else g[st] = g[i] - w[j];
                    dp[st] = dp[i];
                }else if(g[i] < w[j] && dp[st] >= dp[i] + 1){
                    if(dp[st] == dp[i] + 1) g[st] = max(m - w[j] , g[st]);
                    else g[st] = m - w[j];
                    dp[st] = dp[i] + 1;
                }

            }
        }
    }
    cout<

你可能感兴趣的:(dp,算法,动态规划,图论)