HDU3001 三进制的状压dp

该博客讲述了如何使用三进制的动态规划(DP)方法解决旅行者在有限费用下访问所有城市的最优化路径问题。作者通过举例说明了三进制DP的特点和状态转移的注意事项,并指出了在实现代码中的一些额外判断和优化。文章最后提到了题目要求在无法完成旅行时输出-1,这是一个易错点。
摘要由CSDN通过智能技术生成

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

真是打脸
上一个文刚说
“应该不会有三进制这种丧心病狂的东西吧”
立下了这种flag
瞬间就被打脸
这题思路真的不难就是各种套路…感觉很新奇
首先依然是状压dp那习惯的尿性转移
不过这次有了一点不一样的地方

不一样的地方
1.3进制的状压dp有1,2,0这三种状态,因此判断的时候要注意
2.因为有了三种状态,所以要考虑状态转移的时候的限制也不一样了,首先就是例如1121这种东西在从1111转移的时候,要转移的点不能等于1111的最后一个点

然后我这代码写的有点多余..
用了jiance这个函数来确定是否能进行对于最终结果的更新因为必须是各个位上都不是1才可以
讲道理这写的很多余,因为我已经用了save数组存好每个数的三进制形式了

思路嘛…
我没学过tsp是真的
不过写完这个我基本上也会了
就是从你现在所处的点的状态的上一个状态的最后一个点进行转移,你不确定从哪里开始转移,就从每一个点去遍历。

当然这个转移有个条件,那就是
1.你上一个状态必须走过这个点
2.你现在的点必须和上一个状态走过的这个点相连
这个就比较容易判断,我在输入的时候让他等于-1
也把自己和自己连在一起的情况给否定掉了

然后就是坑点
本来我代码根本就没什么错
答案也没说这个在两次根本走不完的情况输出什么
但是实际上他的测试案例要求输出-1

(╯‵□′)╯︵┻━┻

坑死我了

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
using namespace std;
int san[12] = { 1,3,9,27,81,243,729,2187,6561,19683,59049,177147 };
int ss[12] = { 1,4,13,40,121,364,1093,3280,9841,29524,88593,265720 };
int zhi[11][11];
int dp[177148][11];
int save[177148][11];
int biaozhun[13];
int jiance(int qingk, int n)
{
    int biao = biaozhun[n - 1];
    if (qingk >= biao)
    {
        for (int a = n - 1;a >= 0;a--)
        {
            if (qingk >= 2 * san[a])qingk = qingk - 2 * san[a];
            else if (qingk < 2 * san[a] && qingk >= san[a])qingk = qingk - san[a];
            else return 0;
        }
        return 1;
    }
    else return 0;
}
int main()
{
    biaozhun[0] = 1;
    for (int a = 1;a <= 11;a++)biaozhun[a] = san[a] + biaozhun[a - 1];
    for (int a = 1;a <= 177147;a++)
    {
        int jinwei = 1;
        for (int b = 1;b <= 10;b++)
        {
            int y = jinwei + save[a - 1][b];
            if (y >= 3)
            {
                y -= 3;
                jinwei = 1;
            }
            else jinwei = 0;
            save[a][b] = y;
        }
    }
    int n, m;
    while (cin >> n >> m)
    {
        memset(dp, 0, sizeof(dp));
        for (int a = 0;a <= n;a++)for (int b = 0;b <= n;b++) zhi[a][b] = -1;
        for (int a = 0;a < m;a++)
        {
            int q, w, e;
            scanf("%d%d%d", &q, &w, &e);
            if (zhi[q][w] == -1)
            {
                zhi[q][w] = e;
                zhi[w][q] = e;
            }
            else
            {
                zhi[q][w] = min(zhi[q][w], e);
                zhi[w][q] = zhi[q][w];
            }
        }
        /*if (n == 1)
        {
            cout << 0 << endl;
            continue;
        }*/
        int sum = 0x7fffffff;
        for (int a = 1;a <= san[n] - 1;a++)
        {
            for (int b = 1;b <= n;b++)dp[a][b] = 0x7ffffff;
            int jian = 0;
            for (int b = n - 1;b >= 0;b--)
            {
                int tem = san[b];
                if (tem <= a)
                {
                    if (a - jian >= 2 * tem)
                    {
                        int qian = a - tem;
                        jian += 2 * tem;
                        if (qian == 0)dp[a][b + 1] = 0;
                        else
                        {
                            //int ss = 0;
                            for (int d = 1;d <= n;d++)
                            {
                                if (zhi[d][b + 1] != -1 && save[qian][d] != 0)
                                {
                                    if (zhi[b + 1][d] + dp[qian][d] <= dp[a][b + 1])//这里要考虑好如何把不可能到达的dp数据更新成-1
                                    {
                                        //ss = 1;
                                        dp[a][b + 1] = zhi[b + 1][d] + dp[qian][d];
                                        if (jiance(a, n) == 1)sum = min(sum, dp[a][b + 1]);
                                    }
                                }

                            }
                            //if (ss == 0)dp[a][b + 1] = -1;
                        }
                    }
                    else if (a - jian < 2 * tem&&a - jian >= tem)
                    {
                        int qian = a - tem;
                        jian += tem;
                        if (qian == 0)dp[a][b + 1] = 0;
                        else
                        {
                            //int ss = 0;
                            for (int d = 1;d <= n;d++)
                            {
                                if (zhi[d][b + 1] != -1 && save[qian][d] != 0)
                                {
                                    if (zhi[b + 1][d] + dp[qian][d] <= dp[a][b + 1])
                                    {
                                        //ss = 1;
                                        dp[a][b + 1] = zhi[b + 1][d] + dp[qian][d];
                                        if (jiance(a, n) == 1)sum = min(sum, dp[a][b + 1]);
                                    }
                                }
                            }
                            //if (ss == 0)dp[a][b + 1] = -1;
                        }
                    }
                }
            }
        }
        if (sum == 0x7fffffff)sum = -1;
        cout << sum << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值