Imperishable Night(HDU-3001)(状压DP+三进制)

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 

 

题意:有个人想要进行一次旅行,他决定访问n座城市。这个人可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次。n座城市间有m条道路,每条道路都有一个费用。求这个人完成旅行需要花费的最小费用。如果不能完成旅行,则输出-1。

思路:这道题的话,因为城市可以最多被访问两次,所以我们用3进制数来状态压缩,每一位可以存在0,1,2这三个数。然后我们设dp[i][j]表示i状态下以j结尾的最短步数,然后我们再设three[i]表示3的i次方,然后设digit[i][j]表示状态i的第j位是什么数字(0,1,2)。然后我们在压缩的时候注意一下,只要三进制数中存在一个0,那么就说明还有点没有遍历完,就不能当做最终答案来求。然后还要注意因为是3进制,不能方便的判断一串三进制数里面是否存在0,所以在dp过程中需要一边dp一边直接统计结果。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx=60010;
const int inf=0x3f3f3f3f;
using namespace std;
int dp[maxx][15];
int three[15];
int digit[maxx][15];
int a[15][15];
int n,m;
void init()
{
    three[0]=1;
    for(int i=1; i<11; i++)
        three[i]=three[i-1]*3;
    for(int i=0; i<three[10]; i++)
    {
        int ans=i;
        for(int j=0; j<10; j++)
        {
            digit[i][j]=ans%3;
            ans/=3;
        }
    }
}
int main()
{
    init();
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,inf,sizeof(a));
        memset(dp,inf,sizeof(dp));
        for(int i=0; i<m; i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            a[x-1][y-1]=a[y-1][x-1]=min(z,a[x-1][y-1]);
        }
        for(int i=0; i<n; i++)
            dp[three[i]][i]=0;
        int ans=inf;
        for(int j=0; j<three[n]; j++)
        {
            int flag=1;
            for(int i=0; i<n; i++)
            {
                if(digit[j][i]==0)
                    flag=0;
                if(dp[j][i]!=inf)
                {
                    for(int k=0; k<n; k++)
                    {
                        if(a[i][k]!=inf && digit[j][k]!=2)
                            dp[j+three[k]][k]=min(dp[j][i]+a[i][k],dp[j+three[k]][k]);
                    }
                }
            }
            if(flag)
            {
                for(int i=0; i<n; i++)
                    ans=min(ans,dp[j][i]);
            }
        }
        if(ans>=inf)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值