Topic http://acm.hdu.edu.cn/showproblem.php?pid=5691
The state dp,dp[i][j],i represents a state in which the current number is or is not taken, and J indicates the end of the number of J,
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7typedefLong Longll;8ll dp[1<< -][ -];9 Ten intnum[ -],dis[ -],vis[ -]; Onell Max (ll X,ll y) {returnX>y?x:y;} A Constll inf=1e18; - - intMain () the { - intt,n,pos=0; -scanf"%d",&t); - while(t--) + { -scanf"%d",&n); +memset (vis,-1,sizeof(Vis)); A for(intI=0;i< (1<<N); i++) at for(intj=0; j<n;j++) -DP[I][J] =-INF; - for(intI=0; I<n; i++) { -scanf"%d%d",&num[i],&dis[i]); - if(dis[i]!=-1) -vis[dis[i]]=i; in } - if(vis[0]!=-1) dp[(1<<vis[0])][vis[0]]=0;//0 This seat is occupied. to Else{ + for(intI=0; I<n; i++)//No specific location - if(dis[i]==-1) dp[(1<<i)][i]=0;// the } * intCas= (1<<n)-1; $ for(intI=1; I<=cas; i++){Panax Notoginseng intans=0; - for(intj=0; J<n; J + +) the if(i& (1<<j)) +ans++; A if(vis[ans]!=-1) the { +ans=Vis[ans]; - for(intj=0; J<n; J + +) $ if((i& (1<<J)) &&j!=ans) $Dp[i| (1<<ans)][ans]=max (dp[i| (1<<ans)][ans],dp[i][j]+num[j]*Num[ans]); - } - Else the { - for(intj=0; J<n; J + +){Wuyi if(i& (1<<j)) { the for(intk=0; K<n; k++){ - if(! (i& (1<<k))) { WuDp[i| (1<<k)][k]=max (dp[i| (1<<K)][k],dp[i][j]+num[k]*num[j]); - } About } $ } - } - } - } Aprintf"Case #%d:\n",++POS); +ll maxn=-INF; the for(intI=0; I<n; i++) -maxn=Max (maxn,dp[cas][i]); $printf"%i64d\n", MAXN); the } the return 0; the}
View Code
HDU 5691 (pressure dp) sitting in line