HDU 3001 Traveling(状压DP)

来源:互联网 发布:啊哈c语言 pdf微盘 编辑:程序博客网 时间:2024/06/05 15:45

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

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 11 2 1003 21 2 402 3 503 31 2 31 3 42 3 10
 

Sample Output

100907
 


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

原代码链接:http://blog.csdn.net/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;}

0 0
原创粉丝点击