Travelling

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

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
题意:有N个城市,一个人要 经过每个点,且经过每个点不超过2次。求最少的路费。可以从任意一个点开始。
思路:TSP,这个是三进制的状态压缩,因为过每个点要求不超过两次,其余dp的思路和TSP二进制差不多,可以说一样。
dp[i][j]表示在状态i下以j点结束的所要的最小费用。 dp[i][j]=min(dp[i][j],dp[cc][v]+ma[v][j]);cc,表示i的上一个状态。
代码:
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<string.h>
4 #include<iostream>
5 #include<stdlib.h>
6 #include<queue>
7 #include<stack>
8 typedef long long ll;
9 long long ma[20][20];
10 long long dp[60000][20];//开pow(3,11);
11 long long san[60000][20];
12 int a[60000];//记录状态是否都至少经过一次
13 const long long N=1e9;
14 using namespace std;
15 int main(void)
16 {
17 long long n,i,j,k,p,q;
18 long long x,y,v;
19 memset(san,0,sizeof(san));
20 for(i=0; i<60000; i++)
21 {
22 ll vv=i;
23 int flag=0;
24 int f=0;
25 while(vv)
26 {
27 san[i][f++]=vv%3;
28 if(vv%3==0)
29 {
30 flag=1;
31 }
32 vv/=3;
33
34 }
35 if(flag==0) a[i]=1;
36 }//把状态用三进制表示
37 while(scanf("%lld %lld",&p,&q)!=EOF)
38 {
39 for(i=0; i<20; i++)
40 for(j=0; j<20; j++)
41 ma[i][j]=N;
42 for(i=0; i<60000; i++)
43 for(j=0; j<20; j++)
44 dp[i][j]=N;
45 for(i=0; i<q; i++)
46 {
47 scanf("%lld %lld %lld",&x,&y,&k);
48 ma[x-1][y-1]=min(k,ma[x-1][y-1]);
49 ma[y-1][x-1]=min(k,ma[x-1][y-1]);
50 }
51 int xx=1;
52 for(i=0; i<p; i++)
53 {
54 dp[xx][i]=0;
55 xx*=3;
56 }//出发点初始化置0。
57 int yy=1;
58 for(i=0; i<p; i++)
59 {
60 yy*=3;
61 }
62 for(i=1; i<yy; i++)
63 {
64 ll pp=i;
65 for(j=0; j<p; j++)
66 {
67 if(san[pp][j]>0)//判断当前状态是否经过这点
68 {
69 int uu=0;
70 int rr=1;
71 for(v=0; v<p; v++)
72 {
73 int vp=san[pp][v];
74 if(v==j)
75 {
76 vp--;
77 }
78 vp*=rr;
79 rr*=3;
80 uu+=vp;
81 }
82 for(v=0; v<p; v++)
83 {
84 if(san[uu][v]>0)//判断上个状态是否经过上个状态的结束点
85 {
86 dp[i][j]=min(dp[i][j],dp[uu][v]+ma[v][j]);
87
88 }
89 }
90 }
91 }
92
93 }
94 long long minn=N;
95 int tu=0;
96 for(i=0; i<p; i++)
97 {
98 tu*=3;
99 tu+=1;
100 }
101 for(i=tu; i<yy; i++)//至少经过每个点一次中取最小
102 {
103 if(a[i])
104 {
105 for(j=0; j<p; j++)
106 {
107 if(dp[i][j]<minn)
108 {
109 minn=dp[i][j];
110 }
111
112 }
113 }
114
115 }
116
117 if(minn==N)
118 {
119 printf("-1\n");
120 }
121 else
122 printf("%lld\n",minn);
123 }
124 return 0;
125 }

Travelling(hdu3001)的更多相关文章

  1. HDU-3001 Travelling

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

  2. HDU3001 Travelling

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

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

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

  4. [状压dp]HDU3001 Travelling

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

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

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

  6. HDU3001 Travelling 状压DP

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

  7. HDU3001 Travelling (状压DP)

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

  8. 【状压dp】Travelling

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

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

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

随机推荐

  1. 非线性回归支持向量机——MATLAB源码

    支持向量机和神经网络都可以用来做非线性回归拟合,但它们的原理是不相同的,支持向量机基于结构风险最小化理论,普遍认为其泛化能力要比神经网络的强.大量仿真证实,支持向量机的泛化能力强于神经网络,而且能避免 ...

  2. C#序号

    OnRowCreated="gridViewCorrection_RowCreated" <asp:BoundField HeaderText="序号" ...

  3. 为什么重写equals必须重写hashCode

    目录 equals常见面试题 为什么要重写equals 重写equals不重写hashCode会存在什么问题 总结 equals常见面试题 在开始聊之前,我们先看几个常见的面试题,看看你能不能都回答上 ...

  4. javaSE中级篇3——集合体系(另外一种存储容器)——更新完毕

    集合还是一种工具,所以它们的包都在java.util包下 1.集合的整个体系结构(是需要掌握的体系,完全体系不是这样) 对图中所说的 序和重复 这两词的说明: 序:指的是添加进去的元素和取出来的元素 ...

  5. Linux环境下为普通用户添加sudo权限

    系统环境:Centos6.5 1.背景: sudo是Linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部root命令的一个工具.Linux系统下,为了安全,一般来说我们操作都是在普通用户 ...

  6. GO 通过进程号输出运行运行信息

    操作系统应用可以使用PID来查找关于进程本身的信息.当进程失败时获取到的PID就非常有价值,这样就可以使用PID跟踪整个系统中的系统日志,如/var/log/messages./var/log/sys ...

  7. RTTI (Run-time type information) in C++

    In C++, RTTI (Run-time type information) is available only for the classes which have at least one v ...

  8. oracle中注释都是问号?中文显示不出来问题

    本人在工作中需要把开发上的库恢复到自己的虚拟机里面,然而捣鼓了许久建立好数据库之后,在使用建表语句初始化表的时候,发现注释都是????? 然后一脸懵逼不知何解,网上一大堆是说修改环境变量 NLS_LA ...

  9. Android 实现微信QQ分享以及第三方登录

    集成准备 在微信开放平台创建移动应用,输入应用的信息,包括移动应用名称,移动应用简介,移动应用图片信息,点击下一步,选择Android 应用,填写信息提交审核. 获取Appkey 集成[友盟+]SDK ...

  10. Handler与多线程

    1.Handler介绍 在Android开发中,我们常会使用单独的线程来完成某些操作,比如用一个线程来完成从网络上下的图片,然后显示在一个ImageView上,在多线程操作时,Android中必须保证 ...