Dp[i][j][k] represents to the first I position, where I position is j,k for already selected I the number is those (2 binary pressure)
Then: actually really useful state rarely, can write memory search, I write the BFS accelerated
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<string>#include<stack>#include<vector>#include<map>#include<queue>#include<algorithm>#include<utility>using namespaceStd;typedefLong LongLL;Const intn= the;Const intinf=- the* (1e8+5);ConstLL mod=1e9+7;intdp[ -][ -][65537];BOOLinq[ -][ -][65537];inta[ -],p[ -],n;intvis[ -];structnode{intpos,u,cur; //Node (int a,int b,int c) {pos=a;u=b;cur=c;}};queue<Node>Q;intMain () {intt,cas=0; scanf ("%d",&T); while(t--) {printf ("Case #%d:\n",++CAs); memset (Vis,0,sizeof(VIS)); scanf ("%d",&N); for(intI=1; i<=n;++i) {scanf ("%d%d",&a[i],&P[i]); if(p[i]!=-1){ ++P[i]; Vis[p[i]]=i; } } intL= (1<<n)-1; for(intI=0; i<=n;++i) for(intj=0; j<=n;++j) for(intk=0; k<=l;++k) Dp[i][j][k]=INF; memset (INQ,0,sizeof(INQ)); Q.push (node{0,0,0});DP [0][0][0]=0; while(!Q.empty ()) {Node e=Q.front (); Q.pop (); intpos=e.pos+1; if(vis[pos]&& (e.cur& (1<< (vis[pos]-1)))==0){ intaim=e.cur| (1<< (vis[pos]-1)); if(dp[pos][vis[pos]][aim]==INF) Q.push (Node{pos,vis[pos],aim}); Dp[pos][vis[pos]][aim]=max (dp[pos][vis[pos]][aim],dp[e.pos][e.u][e.cur]+a[e.u]*A[vis[pos]]); Continue; } for(intI=1; i<=n;++i) { if(p[i]!=-1)Continue; if(e.cur& (1<< (I-1)))Continue; intaim=e.cur| (1<< (I-1)); if(dp[pos][i][aim]==INF) Q.push (Node{pos,i,aim}); Dp[pos][i][aim]=max (dp[pos][i][aim],dp[e.pos][e.u][e.cur]+a[e.u]*A[i]); } } intans=INF; for(intI=1; i<=n;++i) ans=Max (ans,dp[n][i][l]); printf ("%d\n", ans); } return 0;}
View Code
HDU 5691 sitting in line pressure DP