hdu 3001 Travelling

本文介绍了一个旅行路径规划问题,目标是最小化访问指定城市集合所需的总费用,且每个城市最多只能被访问两次。通过使用三进制状态动态规划的方法来解决这一问题,详细解释了如何预处理输入数据以避免超时,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 

Sample Output
  
100 90 7
 

Source
 

Recommend
gaojie


三进制状态dp

求每个点走两次的tsp,所以用三进制表示每个点走0、1、2次。

预处理,不然会tle。

有重边……

代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define INF 99999999

int dp[100005][12];
int map[15][15];
int three[12];
int n;
int hash[100005][15];

void Init()
{
    int i,p,up;
    three[0]=1;
    for (i=1;i<12;i++)
    {
        three[i]=three[i-1]*3;
    }
    for (i=0;i<three[10];i++)
    {
        p=i;
        up=0;
        while(p!=0)
        {
            hash[i][up++]=p%3;
            p/=3;
        }
    }
}

void Add(int p,int x,int y)
{
    int ret,i,t;
    if (p==0)
    {
        t=1;
        for (i=0;i<n;i++)
        {
            dp[t][i]=0;
            t*=3;
        }
        return;
    }
    if (hash[p][y]==2) return;
    ret=0;
    for (i=n-1;i>=0;i--)
    {
        if (y==i) ret=ret*3+hash[p][i]+1;
        else ret=ret*3+hash[p][i];
    }
  //  printf("%d...%d....%d....%d\n",p,x,y,ret);
    if (dp[ret][y]==-1) dp[ret][y]=dp[p][x]+map[x][y];
    else dp[ret][y]=min(dp[ret][y],dp[p][x]+map[x][y]);
    return;
}

bool Check(int t)
{
    int i;
    for (i=0;i<n;i++)
    {
        if (hash[t][i]==0) return false;
    }
    return true;
}


int main()
{
    int i,j,m,k,x,y,t,ans;
    Init();
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            {
                map[i][j]=INF;
            }
        }
        for (i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&t);
            x--;
            y--;
            map[x][y]=map[y][x]=min(t,map[x][y]);
        }
        memset(dp,-1,sizeof(dp));
        dp[0][0]=0;
        for (i=0;i<three[n];i++)
        {
            for (j=0;j<n;j++)
            {
                if (dp[i][j]==-1) continue;
                for (k=0;k<n;k++)
                {
                    if (j==k) continue;
                    Add(i,j,k);
                }
            }
        }
        ans=INF;
        for (i=0;i<three[n];i++)
        {
            for (j=0;j<n;j++)
            {
             //   printf("%d..%d...%d...%d\n",ans,i,j,dp[i][j]);
                if (dp[i][j]==-1 || dp[i][j]>=ans) continue;
                if (Check(i)==true) ans=dp[i][j];

            }
        }
        printf("%d\n",ans==INF?-1:ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值