hdu 3001 Travelling 【状压dp+三进制】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001


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
 


/*
每个顶点经过最多2次,也就是说有0,1,2三个状态,我们状态转移的时候要用三进制。
另外起点任意,所以dp[three[i]][i]=0,剩下的初始化为inf。状态转移方程为:
dp[nex][k] = min(dp[nex][k], dp[i][j] + load[j][k]);
其中 nex = i + three[k] 因为 nex为 i和three[k]三进制相加对应的十进制
三进制要模拟实现
*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);
const ll mod=1e9+7;
const int M=100000;
const int N=2e5+10;

int mp[12][12];
int n,m,ans;
int three[11]={1,3,9,27,81,243, 729, 2187, 6561, 19683, 59049};
//记录三进制第i位为1其余为0时的十进制对应的值
int dp[N][12];
//第一维时走过的集合状态的十进制,第二维时终点
int num[N][12];
//存三进制,第一维记录三进制对应的十进制,第二维表示对应的三进制的每一位
//左边地位右边高位,列入  num[4][0]=1 num[4][1]=1  11就是4对应的三进制


void init()//计算0 ~ 3^10的三进制
{
    for(int i=0;i<three[10];i++)
    {
        int tmp=i;
        for(int j=0;j<10;j++)
        {
            num[i][j]=tmp%3;
            tmp/=3;
        }
    }
}

int main()
{
    init();
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                mp[i][j]=inf;
        int a,b,c;
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            mp[a-1][b-1]=min(mp[a-1][b-1],c);
            mp[b-1][a-1]=min(mp[b-1][a-1],c);
            //因为题目没说不会有重路,所以要取最小值
        }
        memset(dp,inf,sizeof(dp));
        for(int i=0;i<n;i++)//起点任意,设每个起点初始值为0
            dp[three[i]][i]=0;
        int flag,nex,ans=inf;
        for(int i=0;i<three[n];i++)
        {//
            flag=1;//假设表示所有城市都遍历一次以上
            for(int j=0;j<n;j++)
            {//和二进制一样遍历当前所有终点
                if(num[i][j]==0)//如果有遍历0次的flag=0
                    flag=0;
                if(dp[i][j]==inf)
                    continue;
                for(int k=0;k<n;k++)//
                {
                    if(j==k||num[i][k]>=2||mp[j][k]==inf)
                        continue;
                    nex=three[k]+i;
                    dp[nex][k]=min(dp[nex][k],dp[i][j]+mp[j][k]);
                }    //这点类似floyd最短路算法
            }
            if(flag==1)
            {
                for(int j=0;j<n;j++)
                    ans=min(ans,dp[i][j]);
            }
        }
        if(ans==inf) ans=-1;
        printf("%d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值