HDU 3001 "state compression DP"

Source: Internet
Author: User

Test instructions

Give n points m without an edge.

Each point is required to walk up to two times, to access all points given the minimum sum of weights required for the side of the route.

Ideas:

Tri-State Compression dp,0 representative walked 0 times, by analogy.

The first time to get the three-state compression DP, the sense of focus is the preprocessing of data, the use of the array decomposition of the individual bits, so as to achieve the purpose of similar binary.

Then is the representation of the state, Dp[s][i] represents the optimal value of the state s when it reaches I.

State transfer also at a glance, not nonsense.

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intinf=0x3f3f3f3f;intpho[ the][ the];intunit[ A];intdic[60000][ One];intdp[60000][ One];inlinevoidinit () {memset (dic,0,sizeof(DIC)); unit[0]=1;  for(intI=1; i<= One; i++) {Unit[i]=3*unit[i-1]; }     for(intI=0; i<=59049; i++)    {        inttmp=i;  for(intj=0; tmp;j++) {Dic[i][j]=tmp%3; TMP/=3; }    }}intMain () {intn,m;    Init ();  while(SCANF ("%d%d", &n,&m)! =EOF) {         for(intI=1; i<=n;i++)        {             for(intj=1; j<=n;j++) {Pho[i][j]=inf; } Pho[i][i]=0; }         for(intI=1; i<=m;i++)        {            intA,b,c; scanf ("%d%d%d",&a,&b,&c); PHO[A][B]=pho[b][a]=min (pho[a][b],c); }        intEd=1;  for(intI=1; i<=n;i++) {Ed*=3; } Ed--;  for(ints=0; s<=ed;s++)        {             for(intI=1; i<=n;i++) {Dp[s][i]=inf; }        }        BOOLOK; intans=inf;  for(ints=0; s<=ed;s++) {OK=1;  for(intI=1; i<=n;i++)            {                if(dic[s][i-1])                {                    if(s==unit[i-1]) {Dp[s][i]=0; }                    Else                    {                         for(intk=1; k<=n;k++)                        {                            if(i!=k&& (dic[s][k-1])) {Dp[s][i]=min (dp[s][i],dp[s-unit[i-1]][k]+Pho[k][i]); }                        }                    }                }                Else{OK=0; }            }            if(OK) { for(intI=1; i<=n;i++) {ans=min (ans,dp[s][i]); }            }        }        if(ans>=inf) ans=-1; printf ("%d\n", ans); }}

HDU 3001 "state compression 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.