Los DP valley-like pressure to do title record

P2915 [USACO08NOV] Cow mixed Mixed Up

Face questions

Indeed the title entry shape with pressure dp [i] [j] represents the ending i, when the program state number j, code is as follows:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
const int N=20;
int n,k;
int s[maxn];
long long dp[N][maxn];
long long ans;
int maxx;
int main(){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++) scanf("%d",&s[i]);
    for(int I = 0 ; I <n-; I ++) DP [I] [ . 1 << I] = . 1 ; // initialize 
    Maxx = . 1 << n-; // maximum number of states 
    for ( int ST = 0 ; ST <Maxx ; ST ++) { // enumerate viable state 
        for ( int I = 0 ; I <n-; I ++ ) {
             iF (ST & ( . 1 << I)) { // this state is valid 
                for ( int J = 0 ; J <n-; J ++ ) {
                     IF ((ST & (! . 1 && ABS (S [I] -s [J]) << J))> K) { //By topic conditional 
                        DP [J] [ST | ( . 1 << J)] + = DP [I] [ST]; 
                    } 
                } 
            } 
        } 
    } 
    for ( int I = 0 ; I <n-; I ++ ) { 
        ANS + = DP [I] [( . 1 << n-) - . 1 ]; 
    } 
    the printf ( " % LLD \ n- " , ANS);
     return  0 ; 
}

P1171 salesman problem

Face questions

The same pressure is like manner, DP [i] [j] denotes the i-th current city, state j is the shortest, opened O2

// luogu-enable-O2-judger 
#include <bits / STDC ++ H.> The using namespace STD;
 const int N = 25 ;
 const int MAXN = 1.10001 million ;
 int DP [N] [MAXN]; // i-th cities , through the collection of urban J int G [N] [N]; 
 int n-;
 int ANS = 2E9;
 int main () { 
    Scanf ( " % D " , & n-);
     for ( int I = . 1 ; I < n-=; I ++ ) {
         for ( int J =
   
1;j<=n;j++){
            scanf("%d",&g[i][j]);
        }
    }
     memset(dp,88,sizeof(dp));
    dp[1][1]=0;
    for(int k=0;k<=(1<<n)-1;k++){
        for(int i=1;i<=n;i++){
            if(((1<<i-1)&k)){
                for(int j=1;j<=n;j++){
                    if( !( (1<<j-1)&k) ){
                        dp[j][k|(1<<j-1)]=min(dp[j][k|(1<<j-1)],dp[i][k]+g[i][j]);
                    }
                }
            }
        }
    }
    for(int i=2;i<=n;i++) ans=min(dp[i][(1<<n)-1]+g[i][1],ans);
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LJB666/p/11223989.html