HDU3001 三进制的状压dp

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,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值