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

Travelling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8286    Accepted Submission(s): 2703

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
 
 
 
 
题解:
1.对于每一个点, 有三种状态:没有被访问、被访问一次、被访问两次。所以需要用三进制进行压缩。
2.dp[status][last]表示:在状态为status下(即走了多少个点,以及每个点走了多少次),最后走的是last点的最小花费。
3.对于一个已有状态:dp[s][j],枚举每一个点k,如果这个点k不是j,而k、j有边,且k被访问的次数不超过2次,那么下一步就可以访问k点。
4.类似的题目:HDU4856
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = +; int n, m;
int g[MAXN][MAXN], c[MAXN], dp[][]; int main()
{
c[] = ;
for(int i = ; i<=; i++) //计算三进制
c[i] = c[i-]*; while(scanf("%d%d", &n, &m)!=EOF)
{
memset(g, -, sizeof(g));
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u--; v--;
if(g[u][v]!=-) w = min(w, g[u][v]);
g[u][v] = g[v][u] = w;
} memset(dp, -, sizeof(dp));
for(int i = ; i<n; i++)
dp[c[i]][i] = ; int ans = INF;
for(int s = ; s<c[n]; s++)
for(int j = ; j<n; j++)
{
if(dp[s][j]==-) continue; //此状态不存在, 跳过 int cnt = ;
for(int k = ; k<n; k++)
{
if(j!=k && g[j][k]!=- && (s/c[k])%< )
{
int tmp = s + c[k]; //递推出下一个状态
if(dp[tmp][k]==- || dp[tmp][k]>dp[s][j]+g[j][k]) //更新
dp[tmp][k] = dp[s][j] + g[j][k];
} if((s/c[k])%) cnt++; //统计状态s下走了多少个点
} if(cnt==n) ans = min(ans, dp[s][j]); //如果状态s下走完所有点, 则更新答案
}
printf("%d\n", ans==INF?-:ans);
}
}

HDU3001 Travelling —— 状压DP(三进制)的更多相关文章

  1. HDU - 3001 Travelling 状压dp + 三进制 [kuangbin带你飞]专题二

    终于刷完搜索专题了. 题意:给定n个城市,每个城市参观不能超过两次,两个城市之间有道路通过需要花费X,求通过能所有城市的最小花费. 思路:每个城市有三个状态0,1,2,可用三进制存储所有城市的访问状态 ...

  2. Travelling(HDU3001+状压dp+三进制+最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目: 题意:n个城市,m条边,每条边都有一个权值,问你经过所有的城市且每条边通过次数不超过两次 ...

  3. HDU3001 Traveling (状压dp+三进制+Tsp问题总结)

    (1)这道题最多可以走两次,所以有0, 1, 2三种状态,所以我们要用三进制 如果要用三进制,就要自己初始化两个数组, 一个是3的n次方,一个是三进制数的第几位的数字是什么 void init() { ...

  4. HDU3001 Travelling 状压DP

    哭瞎啊,每一个城市能够经过至多两次,但没有要求必须经过两次.想用 两个状压来乱搞搞.结果自觉得会T.结果 WA了,搞了一下午.没想到用三进制啊.智商捉急,參考了 http://blog.csdn.ne ...

  5. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  6. hdu3001(状压dp,三进制)

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. hdu3001(状压dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:n 个城市已经 m 条路 以及对应路费 c,要求遍历所有城市最少的路费,每个城市不能超过2 ...

  8. HDU 3001 Travelling (状压DP + BFS)

    题意:有一个人要去旅游,他想要逛遍所有的城市,但是同一个城市又不想逛超过2次.现在给出城市之间的来往路费,他可以选择任意一个点为起点. 问逛遍所有城市的最低路费是多少. 析:用三进制表示每个城市的访问 ...

  9. hdu 3001 Travelling 经过所有点(最多两次)的最短路径 三进制状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求从任意一个点出发,经过所有点的最短路径长度(每个点至多可以经过两次). 思路 状态表示.转移及大体思路 与 poj 3311 Hie with the ...

随机推荐

  1. Vue微信自定义分享时安卓系统config:ok,ios系统config:invalid signature签名错误,或者安卓和ios二次分享时均config:ok但是分享无效的解决办法

    简述需求:要求指定页面可以进行微信自定义分享(自定义标题,描述,图片,链接),剩下的页面隐藏所有基础接口.二次分享依然可以正常使用,切换至其他页面也可以正常进行自定义分享. 这两天在做微信自定义分享的 ...

  2. Device eth0 does not seem to be present,delaying initialization问题

    1.打开/etc/udev/rules.d/70-persistent-net.rules: cat /etc/udev/rules.d/70-persistent-net.rules 文件内容如图: ...

  3. Web安全解决方案

    什么是 .NET Framework 安全性? .NET Framework 提供了用户和代码安全模型,允许对用户和代码可以执行的操作进行限制.要对基于角色的安全性和代码访问安全性进行编程,可以从 S ...

  4. xtu DP Training C.炮兵阵地

    炮兵阵地 Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 11856 ...

  5. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

  6. [Vijos1617] 超级教主(DP + 单调队列)

    传送门 设 f[i] 表示吃完 f[i] 及其以下的能量球后所剩下的能量. 所以 f[i] = max(f[i], f[j] + (sum[i] - sum[j]) - i * 100) ( 0 &l ...

  7. jQuery根据属性模糊匹配元素

    1.查看带有指定属性的元素: [attribute] 例如: $("div[id]") 2.查看属性值是某个特定值的元素: [attribute=value] 例如: $(&quo ...

  8. Spring中基于AOP的XML架构

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/xml-schema-based-aop-wi ...

  9. LINUX下安装和配置WEBLOGIC10.0.3

    weblogic for linux安装 首先声明,我参考了某位原创者的笔记,加以整理的.安装1. 安装前的准备工作1.1 首先请确认您要安装的Weblogic版本所在的平台已通过了BEA的认证,完整 ...

  10. python字符串连接方法效率比较

    方法1:直接通过加号(+)操作符连接 1 website = 'python' + 'tab' + '.com' 方法2:join方法 1 2 listStr = ['python', 'tab',  ...