HDU 3001 Traveling(状压DP)

本文介绍了一个旅行商问题(TSP)的特殊版本,其中每个城市最多只能被访问两次。通过使用3进制状态压缩的方法来求解该问题,并提供了一段C++代码实现,该算法能够找到最小费用路径或判断无法完成旅行的情况。

D - Travelling
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 


【题目大意】10个点的TSP问题,但是要求每个点最多走两边,不是只可以走一次,所以要用三进制的状态压缩解决这个问题。可以预处理每个状态的第k位是什么。

原代码链接:http://blog.youkuaiyun.com/accry/article/details/6607703

3进制,代表走过这个点的次数


#include <cstdio>
#include<cstdlib>
#include <cstring>
#define INF 0x1f1f1f1f
#define min(a,b) (a) < (b) ? (a) : (b)
using namespace std;

int N,M;
int tri[12] = {0,1,3,9,27,81,243,729,2187,6561,19683,59049};
int dig[59050][11];  //dig[state][k_dig]  状态state的第k位是多少
int edge[11][11],dp[59050][11];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
//    freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif // ONLINE_JUDGE

//
//    char stit[200];
//    for (int i=0;i<12;i++)
//    {
//        itoa(tri[i],stit,3);
//        puts(stit);
//    }


    for(int i = 0; i < 59050; ++i)
    {
        int t = i;
        for(int j = 1; j <= 10; ++j)
        {
            dig[i][j] = t%3;
            t /= 3;
            if(t == 0)break;
        }
    }

//    itoa(123,stit,3);
//    puts(stit);
//
//    puts("tst");
//    for(int j = 1; j <= 10; ++j)
//        printf("%d ",dig[123][j]);


    while(scanf("%d%d",&N,&M) != EOF)
    {
        memset(edge,INF,sizeof(edge));

        int a,b,c;
        int m2=M;
        while(M --)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(c < edge[a][b])edge[a][b] = edge[b][a] = c;
        }

//        for (int i=1;i<=N;i++)
//        {
//            for (int j=1;j<=N;j++)
//                printf("%d ",edge[i][j]);
//            puts("");
//        }

        memset(dp,INF,sizeof(dp));

        for(int i = 1; i <= N; ++i)dp[tri[i]][i] = 0;

        int ans = INF;
        for(int S = 0; S < tri[N+1]; ++S)
        {
            int visit_all = 1;
            for( int i = 1; i <= N; ++i)
            {
                if(dig[S][i] == 0)visit_all = 0;
                if(dp[S][i] == INF)continue;

                for(int j = 1; j <= N; ++j)
                {
                    if(i == j)continue;
                    if(edge[i][j] == INF ||dig[S][j] >= 2)continue;
                    int newS = S + tri[j];
                    dp[newS][j] =min(dp[newS][j],dp[S][i] + edge[i][j]);
                }
            }

            if(visit_all)
            {
                for(int j = 1; j <= N; ++j)
                    ans = min(ans,dp[S][j]);
            }

        }

        if(ans == INF)
        {
            puts("-1");
            continue;
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值