zoukankan      html  css  js  c++  java
  • 三进制状态压缩DP(旅行商问题TSP)HDU3001

    http://acm.hdu.edu.cn/showproblem.php?pid=3001

    Travelling

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3864    Accepted Submission(s): 1217


    Problem Description
    After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
     

    Input
    There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.
     

    Output
    Output the minimum fee that he should pay,or -1 if he can't find such a route.
     

    Sample Input
    2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3 10
     

    Sample Output
    100 90 7
    题意:给出一个无向图,并给出路径及费用,问旅行完所有的节点需要的花费最少是多少(可以从任意一点出发,每个节点经过的次数不超过2次)

    分析:对于只经过节点仅且一次的题目,很清楚用二进制dp,暴搜完所有的状态,而此题每个节点有三个状态,即没走过,走了一次,走了两次,所以用三进制表示,分别代表三种状态;用dp[i][j]表示经过节点j后到达状态i;当i状态满足所有位置没有0是,即每个点都至少经过了一次,就是一种情况,但不一定是最优情况所以要更新最小值;

    程序:

    #include"stdio.h"
    #include"string.h"
    #include"iostream"
    #include"map"
    #include"string"
    #include"queue"
    #include"stdlib.h"
    #include"algorithm"
    #include"math.h"
    #define M 60001
    #define eps 1e-10
    #define inf 100000000
    #define mod 100000000
    #define INF 0x3f3f3f3f
    using namespace std;
    int dp[M][12],px[12],a[M][12],dis[12][12],path[M][12];
    void init()
    {
        int i;
        px[0]=1;
        for(i=1;i<=10;i++)
            px[i]=px[i-1]*3;
        memset(a,0,sizeof(a));
        for(i=0;i<px[10];i++)
        {
            int k=i,t=0;
            while(k)
            {
                a[i][t++]=k%3;
                k/=3;
            }
        }
    }
    int main()
    {
        init();
        int n,m,i,j,k;
        while(scanf("%d%d",&n,&m)!=-1)
        {
            memset(dis,INF,sizeof(dis));
            for(i=1;i<=m;i++)
            {
                int u,v,c;
                scanf("%d%d%d",&u,&v,&c);
                u--;
                v--;
                if(dis[u][v]>c)
                    dis[u][v]=dis[v][u]=c;
            }
            memset(dp,INF,sizeof(dp));
            memset(path,-1,sizeof(path));
            for(i=0;i<n;i++)
            {
                dp[px[i]][i]=0;
                path[px[i]][i]=-1;
            }
            int ans=INF;
            int I,J;
            for(i=1;i<px[n];i++)
            {
                int flag=1;
                for(j=0;j<n;j++)
                {
                    if(a[i][j]==0)
                    {
                        flag=0;continue;
                    }
                    int cur=i-px[j];
                    for(k=0;k<n;k++)
                    {
                        if(dp[i][j]>dp[cur][k]+dis[k][j])
                        {
                            dp[i][j]=dp[cur][k]+dis[k][j];
                            path[i][j]=k;
                        }
    
                    }
                }
                if(flag)
                {
                    for(j=0;j<n;j++)
                    {
                        if(ans>dp[i][j])
                        {
                            I=i;
                            J=j;
                            ans=dp[i][j];
                        }
    
                    }
                }
            }
            /*********路径**********/
            /*printf("%d->",J+1);
            for(k=path[I][J];k!=-1;k=path[I][k])
            {
                printf("%d->",k+1);
                I=I-px[J];
                J=k;
            }
            printf("
    ");*/
            /***********************/
            if(ans<INF)
            printf("%d
    ",ans);
            else
                printf("-1
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    7-5 幸运彩票 (15分)
    基于Python实现学生管理系统
    NB-IoT模块烧写详细过程
    IAR软件使用的快捷键配置以及配置cc2530环境
    7-54 求方程的解 (10 分)
    7-52 计算球体积 (10 分)
    7-51 号码牌的制作 (10 分)
    7-48 输出星期名缩写 (70 分)
    7-49 求前n项的阶乘之和 (15 分)
    7-46 jmu-python-求单词长度 (10 分)
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348147.html
Copyright © 2011-2022 走看看