Hdu 5691 sitting in line pressure DP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.