HDU 3001 (Pressure dp+ three-in)

Source: Internet
Author: User

Regardless of the binary, it is a logical concept, (last six is used to convert multidimensional data) The core idea is the TSP. The preprocessing here is quite ingenious, calculating the modulus vis[][on each bit in each State].

TSP:DP[I][J] In the I state, the optimal solution with J ending. Two kinds of transfer are: I for everyone, everyone for me.


#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define MAXN 60000 #define INF 0x3f3f3f3fconst double eps=1e-8;using namespace Std;int dp[maxn][12];int maps[13][12];int vis[maxn][12];int    State[12];int n,m;void init () {state[0]=1;    for (int i=1;i<11;i++) state[i]=state[i-1]*3;        for (int i=0;i<=state[10];i++) {int x=i;    for (int j=0;j<10;j++) vis[i][j]=x%3,x/=3;    }}int Main () {//Freopen ("In.txt", "R", stdin);        while (~SCANF ("%d%d", &n,&m)) {init ();        memset (maps,0x3f,sizeof (maps));            for (int i=0;i<m;i++) {int u,v,k;            scanf ("%d%d%d", &u,&v,&k);        Maps[u-1][v-1]=maps[v-1][u-1]=min (MAPS[U-1][V-1],K);        } int tag,ans=inf;        Memset (Dp,0x3f,sizeof (DP));        for (int i=0;i<n;i++) dp[state[i]][i]=0;            for (int i=0;i<state[n];i++) {tag=1;  for (int j=0;j<n;j++)          {if (vis[i][j]==0) tag=0;//This judgment is particularly ingenious, because J will loop, inside the case where I is stored if (dp[i][j]==inf) continue; for (int k=0;k<n;k++) {if (k!=j && vis[i][k]<2 && ma Ps[k][j]!=inf) Dp[i+state[k]][k]=min (dp[i+state[k]][k],dp[i][j]+maps[j][k]);//The last written one is the back, this is the rear Forward, better to write.        Everyone for me, I am for everyone} if (tag) for (int j=0;j<n;j++) ans=min (Ans,dp[i][j]);        } if (ans = = inf) ans =-1;    cout<<ans<<endl; } return 0;}



HDU 3001 (Pressure dp+ three-in)

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.