Hdoj 3001 travelling [3 hexadecimal + traveling salesman]

Source: Internet
Author: User

Question: hdoj 3001 travelling


Question: The standard traveling salesman adds a sentence, and each vertex can take up to two times.


Analysis: The state transition equation is exactly the same, but it only requires a three-digit system, because each point has three states: 0, 1, 2.

Define the State: DP [st] [I]: the minimum cost of the current I point when the State is St

Transition equation: DP [now] [J] = min (DP [now] [J], DP [st] [I] + MP [I] [J ]); now is the status that St can transfer at a time.

Pay attention to initialization.


Analysis:

#include <cstdio>#include <algorithm>#include <cstring>#include <string>#include <iostream>#include <vector>using namespace std;const int inf = 0x3f3f3f3f;const int N = 12;int mp[N][N];int n,m;int bit[N],v[N];int dp[60000][N];void isit(){    bit[0]=1;    for(int i=1;i<N;i++)        bit[i]=bit[i-1]*3;}void solve(int st){    memset(v,0,sizeof(v));    int tmp=0;    while(st)    {        v[tmp++]=(st%3);        st/=3;    }}int count(){    int ans=0;    for(int i=0;i<n;i++)        ans+=(v[i]*bit[i]);    return ans;}int main(){    isit();    while(~scanf("%d%d",&n,&m))    {        memset(mp,inf,sizeof(mp));        for(int i=0;i<m;i++)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            x--,y--;            mp[x][y]=mp[y][x]=min(z,mp[x][y]);        }        memset(dp,inf,sizeof(dp));        for(int i=0;i<n;i++)        {            dp[bit[i]][i]=0;        }        int ans=inf;        for(int st=0;st<bit[n];st++)        {            int ok=1;            solve(st);            for(int i=0;i<n;i++)            {                if(v[i]==0)                    ok=0;                if(dp[st][i]==inf)                    continue;                for(int j=0;j<n;j++)                {                    if(v[j]==2 || i==j)                        continue;                    if(mp[i][j]==inf)                        continue;                    v[j]++;                    int no=count();                    dp[no][j]=min(dp[no][j],dp[st][i]+mp[i][j]);                    v[j]--;                }            }            if(ok)                for(int i=0;i<n;i++)                    ans=min(ans,dp[st][i]);        }        if(ans==inf)            ans=-1;        printf("%d\n",ans);    }    return 0;}


Hdoj 3001 travelling [3 hexadecimal + traveling salesman]

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.