HDU 5691 (pressure dp) sitting in line

Source: Internet
Author: User

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&LT;&LT;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&LT;&LT;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&LT;&LT;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

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.