zoukankan      html  css  js  c++  java
  • hdu 3001 Travelling 旅行商问题变形 三进制的压缩dp

    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,把状态压缩成一个三进制整数,其他地方和旅行商问题一样。

    只要想到三进制表示状态,那么这道题就非常简单了。

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const long long INF = 1000000000000000000;
    const int MAXN = 12;
    long long d[MAXN][MAXN];
    long long dp[59100][MAXN];
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF) {
            memset(d,-1,sizeof(d));
            memset(dp,-1,sizeof(dp));
            int a,b;
            long long c;
            for(int i=0;i<m;i++) {
                scanf("%d%d%I64d",&a,&b,&c);
                if(d[a][b]==-1) d[a][b] = d[b][a] = c;
                else d[a][b] = d[b][a] = min(d[a][b],c);
            }
            for(int j=1;j<=n;j++) {
                int k,zt;
                for(k=1,zt=1;k<j;k++,zt*=3);
                dp[zt][j]=0;
            }
            int ZT=2;
            for(int i=1;i<n;i++) {
                ZT*=3;
                ZT+=2;
            }
            for(int zt=1;zt<=ZT;zt++) {
            int tzt = zt*3,tj=1;
            for(int j=1;j<=n;j++) {
                tj*=3;
                tzt/=3;
                if(dp[zt][j]==-1)
                    dp[zt][j] = INF;
                else continue;
                if(tzt%3==0) continue;
    
                int ttzt = zt;
                for(int k=1;k<=n;k++) {
                    if(ttzt==0) break;
                    if(k!=j&&(ttzt%3)&&d[k][j]!=-1) {
                        dp[zt][j] = min(dp[zt][j],dp[zt-tj/3][k]+d[k][j]);
                    }
                    ttzt/=3;
                }
            }
            }
    
            long long ans=INF;
            int zt=ZT/2;
            for(;zt<=ZT;zt++) {
                int t=zt;
                while(t) {
                    if(t%3==0) goto NEXT;
                    t/=3;
                }
                for(int j=1;j<=n;j++) {
                    if(dp[zt][j]<ans) ans=dp[zt][j];
                }
                NEXT:;
            }
            if(ans==INF) puts("-1");
            else printf("%I64d
    ",ans);
        }
    }
  • 相关阅读:
    金牙与肉屑
    科学研究的动机以及雄心
    适度的自我吹嘘
    ubuntu12启用root账户
    有很多文件夹是受系统保护的
    vs2012换肤功能,vs2012主题及自定义主题
    Ubuntu navicat for mysql 安装和使用
    Asp.Net MVC4 Bundle捆绑压缩技术
    64位Windows Jmail组件报错解决方案
    C#检测上传文件的真实类型
  • 原文地址:https://www.cnblogs.com/lastone/p/5319201.html
Copyright © 2011-2022 走看看