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

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

看到点数就会想到状压,然而题目限制每个城市不能经过超过两次,二进制难以表示——那就用三进制表示!

(其实刚开始的想法是二进制相邻两位表示一个城市的到达状态,然而那样1<<20的数组范围吃不消)

除了三进制以外,这题和普通的状压求最短路没啥差别

  1. /*by SilverN*/
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<cmath>
  7. #include<vector>
  8. using namespace std;
  9. const int mxn=;
  10. int read(){
  11. int x=,f=;char ch=getchar();
  12. while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
  13. while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
  14. return x*f;
  15. }
  16. int f[][],b[];
  17. int n,m;
  18. int mp[][];
  19. int t[][];
  20. void init(){
  21. for(int i=;i<;i++){
  22. int tmp=i;
  23. for(int j=;j<;j++){
  24. t[i][j]=tmp%;
  25. tmp/=;
  26. }
  27. }
  28. return;
  29. }
  30. int main(){
  31. int i,j,k,u,v,w;
  32. b[]=;
  33. for(i=;i<;i++)b[i]=b[i-]*;
  34. init();
  35. while(scanf("%d%d",&n,&m)!=EOF){
  36. memset(mp,0x3f,sizeof mp);
  37. memset(f,0x3f,sizeof f);
  38. for(i=;i<=m;i++){
  39. u=read();v=read();w=read();
  40. mp[u][v]=mp[v][u]=min(mp[u][v],w);
  41. }
  42. for(i=;i<=n;i++){
  43. f[b[i]][i]=;
  44. }
  45. int ans=0x3f3f3f3f;
  46. int ed=b[n+]-;
  47. for(i=;i<=ed;i++){
  48. bool all=;
  49. for(j=;j<=n;j++){
  50. if(!t[i][j]){
  51. all=;continue;
  52. }
  53. for(k=;k<=n;k++){
  54. if(j==k)continue;
  55. if(t[i][k]>)continue;
  56. f[i+b[k]][k]=min(f[i+b[k]][k],f[i][j]+mp[j][k]);
  57. }
  58. }
  59. if(all){
  60. for(j=;j<=n;j++)
  61. ans=min(ans,f[i][j]);
  62. }
  63. }
  64. if(ans==0x3f3f3f3f)ans=-;
  65. printf("%d\n",ans);
  66. }
  67. return ;
  68. }

HDU3001 Travelling的更多相关文章

  1. HDU-3001 Travelling

    http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ...

  2. HDU3001 Travelling —— 状压DP(三进制)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/ ...

  3. [状压dp]HDU3001 Travelling

    题意: 走n个城市, m条路, 起点任意, 每个城市走不超过两次, 求最小花费, 不能走输出-1. $1\le n\le 10$ 分析: 每个城市的拜访次数为0 1 2, 所以三进制状压, 先预处理1 ...

  4. HDU3001 Travelling 状压DP

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

  5. HDU3001 Travelling (状压DP)

    题目没有起点限制,且每个节点至少访问1次,最多访问2次,所以用三进制数表示节点的状态(选取情况). 因为三进制数的每一位是0或1或2,所以预处理z状态S的第j位的数是有必要的. 边界条件:dp[tri ...

  6. 【状压dp】Travelling

    [hdu3001]Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. Travelling(hdu3001)

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

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

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

  9. ACM: 限时训练题解- Travelling Salesman-最小生成树

    Travelling Salesman   After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...

随机推荐

  1. Expect自动化交互程序

    Expect介绍: 1.什么是Expect Expect是一个用来实现自动化交互功能的软件套件,基于TCL的脚本编程工具语言,方便学习,功能强大. 2.为什么要使用expcet: 当今的企业运维中,自 ...

  2. yii2 基本的增删改查

    一:添加方法 1.1 使用成员属性的方式 save $user_name = $_POST['user_name']; $password = $_POST['password']; //实例化 $u ...

  3. 日志收集系统Flume及其应用

    Apache Flume概述 Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统.Flume 支持定制各类数据发送方,用于收集各类型数据:同时,Fl ...

  4. kubectl alias auto complete

    平时kubectl命令管理kubernetes,敲久了就觉得比较麻烦,想着使用alias k来代替kubectl,可是当输入k时没有了自动补全的功能 这里在 ~/.bashrc 添加如下配置后,可以自 ...

  5. Xadmin后台管理系统搭建基于Django1.11.11+Python3.6

    安装python及Django百度即可 主要介绍Xadmin安装 访问地址:https://github.com/sshwsfc/xadmin  下载 安装好之后,将xamdin目录复制到项目 我放在 ...

  6. Codeforces Round #460 (Div. 2)-B. Perfect Number

    B. Perfect Number time limit per test2 seconds memory limit per test256 megabytes Problem Descriptio ...

  7. Codeforces Round #456 (Div. 2) A. Tricky Alchemy

    传送门:http://codeforces.com/contest/912/problem/A A. Tricky Alchemy time limit per test1 second memory ...

  8. Keepalivaed +Nginx proxy 高可用架构方案与实施过程细节

    1.开源产品介绍 1)CMS介绍 官方网站http://www.dedecms.com/,是一个网站应用系统构建平台,也是一个强大的网站内容管理系统,既可以用来构建复杂的体系的企业信息门户或者电子商务 ...

  9. [原]sencha touch之carousel

    carousel组件是个非常不错的东东,自带可滑动的效果,效果如下图 上面部分可以左右滑动,下面部分可以上下滑动,效果还是不错的,app程序中很有用的布局 代码如下: Ext.application( ...

  10. "帮你"-用户模板和用户场景

    场景/故事/story 典型用户: 用户性质 典型用户介绍 姓名 小李 年龄 20岁 职业 学生 代表的用户在市场上的比例和重要性 代表学校内广大普通学生,因此有很大的重要性. 使用本软件的典型场景 ...