hdu 3001 Travelling (旅行商问题)

7 篇文章 0 订阅

题目链接

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

题意:给一个n,m,n代表有n个城市,m代表有m条路,下一行给你m条路连通的城市以及对应的路费(当然是双向),问你从任意个城市开始参观,每个城市最多只能参观2次,问你参观完所有城市需要的经费。
分析:由于每个城市不再是只能去一次,所以二进制的状压肯定是不行的了,所以我们应该考虑三进制的,大体思路和二进制的一样,只是三进制的在写的时候有些地方不一样而已。
#include<string.h>
#include<stdio.h>
#include<algorithm>
#define inf 1e12
#define ll long long
using namespace std;
ll n,m;
ll a[15][15];
ll c[15];///存的是3的0次方到10次方的值
ll state[600000][15];///存的是n次方以内的所有状态,将这些状态保存在第二维中
ll dp[600000][15];
void init()
{
    c[0]=1;
    for(ll i=1;i<=n;i++) c[i]=c[i-1]*3;
    for(ll i=0;i<c[n];i++)///预处理每一个状态包含的0 1 2的情况,共n位,因为需要n位
    {
        ll j=0,x=i;
        while(j<n)
        {
            state[i][j]=x%3;
            x/=3;
            j++;
        }
    }
    for(ll i=0;i<n;i++)
        for(ll j=0;j<n;j++)
        a[i][j]=inf;
    for(ll i=0;i<c[n];i++)
        for(ll j=0;j<n;j++)
        dp[i][j]=inf;
    for(ll i=0;i<n;i++)
        dp[c[i]][i]=0;///因为是从任意点开始都行,所以在初始化时要把从第0个点,第1个点......第n-1各点都初始化为0,如果是必须从第0各点开始,则只需要使dp[1][0]=0即可
}
int main()
{
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        init();
        for(ll i=1;i<=m;i++)
        {
            ll x,y,cc;
            scanf("%lld%lld%lld",&x,&y,&cc);
            x--,y--;
            a[x][y]=a[y][x]=min(a[x][y],cc);///防止有重边
        }
        ll ans=inf;
        for(ll i=0;i<c[n];i++)
        {
            ll flog=1;
            for(ll j=0;j<n;j++)
            {
                if(!state[i][j]) flog=0;
                for(ll k=0;k<n;k++)
                {
                    if(state[i][k]>=2||k==j||a[j][k]==inf) continue;///此时想从j到k,那么k这个状态必须是2以下,要不然不能去
                    dp[i+c[k]][k]=min(dp[i+c[k]][k],dp[i][j]+a[j][k]);
                }
            }
            if(flog)///flog为1代表所有城市都至少去了一次,因此答案ans可以更新
            {
                for(ll j=0;j<n;j++)
                    ans=min(ans,dp[i][j]);
            }
        }
        if(ans==inf) puts("-1");
        else printf("%lld\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值