Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=5691
Exercises
Similar to the TSP with the same pressure, is fixed a number of access order.
DP[I][J] represents the maximum product sum of the state of the first CNT point that is filled with state I and the last J.
Then there are dp[i| ( 1<<k)][k]=max (dp[i| ( 1<<k)][k],dp[i][j]+a[j]*a[k]).
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespacestd;Const intMAXN = A;Const intINF =2e9;intdp[1<< -][ A];intcnt[1<< -];intA[MAXN], P[MAXN],F[MAXN];intN;voidPre () { for(inti =0; I < (1<< -); i++) {Cnt[i]=0; for(intj =0; J < -; J + +) { if(i& (1<< j)) cnt[i]++; } }}voidinit () { for(inti =0; I < (1<< N); i++) { for(intj =0; J <= N; J + +) {Dp[i][j]= -INF; }} memset (F,-1,sizeof(f));intMain () {pre (); intTc,kase=0; scanf ("%d", &TC); while(tc--) {scanf ("%d", &N); Init (); for(inti =0; I < n; i++) {scanf ("%d%d", A + I, p +i); if(P[i]! =-1) F[p[i]] =i; } A[n]=0; P[n] =N; dp[0][n] =0; for(inti =0; I < (1<< N); i++) { intsum =Cnt[i]; for(intj =0; J <= N; J + +) { if((i& (1<< j) = =0&&j!=n)Continue; //Restricted points: if(F[sum]! =-1) { if((i& (1<< F[sum]) = =0) {Dp[i| (1<< F[sum])][f[sum] =Max (Dp[i| (1<< F[sum])][f[sum], dp[i][j]+a[j]*A[f[sum]]); } } Else { //points that can be moved freely for(intK =0; K < n; k++) { if(i& (1<< k))Continue; if(P[k] ==-1) {Dp[i| (1<< k)][k] = max (Dp[i | (1<< k)][k], Dp[i][j] + a[j] *A[k]); } } } } } intAns =-INF; for(intj =0; J < N; J + +) ans = max (ans, dp[(1<< N)-1][j]); printf ("Case #%d:\n", ++Kase); printf ("%d\n", ans); } return 0;}
Hdu 5691 sitting in line pressure DP