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的数组范围吃不消)

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

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int f[][],b[];
int n,m;
int mp[][];
int t[][];
void init(){
for(int i=;i<;i++){
int tmp=i;
for(int j=;j<;j++){
t[i][j]=tmp%;
tmp/=;
}
}
return;
}
int main(){
int i,j,k,u,v,w;
b[]=;
for(i=;i<;i++)b[i]=b[i-]*;
init();
while(scanf("%d%d",&n,&m)!=EOF){
memset(mp,0x3f,sizeof mp);
memset(f,0x3f,sizeof f);
for(i=;i<=m;i++){
u=read();v=read();w=read();
mp[u][v]=mp[v][u]=min(mp[u][v],w);
}
for(i=;i<=n;i++){
f[b[i]][i]=;
}
int ans=0x3f3f3f3f;
int ed=b[n+]-;
for(i=;i<=ed;i++){
bool all=;
for(j=;j<=n;j++){
if(!t[i][j]){
all=;continue;
}
for(k=;k<=n;k++){
if(j==k)continue;
if(t[i][k]>)continue;
f[i+b[k]][k]=min(f[i+b[k]][k],f[i][j]+mp[j][k]);
}
}
if(all){
for(j=;j<=n;j++)
ans=min(ans,f[i][j]);
}
}
if(ans==0x3f3f3f3f)ans=-;
printf("%d\n",ans);
}
return ;
}

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. grep过滤目录或文件方法

    在使用grep在指定目录下查找包含指定字符串的文件是,我们想过滤(即不递归查询指定目录)时!可以使用 –exclude-dir 参数 单个目录实例 搜索.目录但不搜索在.目录下的.svg目录中包含&q ...

  2. HDU-3366-Count the string(KMP,DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. Windows10系统下查看mysql的端口号并修改

    mysql的端口号默认是3306,初学者可能有时会忘记或者之前修改了默认的端口号,忘记了,或者很多时候我们一台电脑需要安装两个mysql或者想设置一个自己的喜欢的数字,那么接下来我们来看看如何查看或者 ...

  4. 3- vue django restful framework 打造生鲜超市 - model设计和资源导入

    3- vue django restful framework 打造生鲜超市 - model设计和资源导入 使用Python3.6与Django2.0.2(Django-rest-framework) ...

  5. 八、Shell test 命令

    Shell test 命令 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 数值测试 参数 说明 -eq 等于则为真 -ne 不等于则为真 -gt ...

  6. ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值

    <?php class ReturnController extends AbstractActionController { public function returnAction() { ...

  7. 使用Spark Streaming + Kudu + Impala构建一个预测引擎

    随着用户使用天数的增加,不管你的业务是扩大还是缩减了,为什么你的大数据中心架构保持线性增长的趋势?很明显需要一个稳定的基本架构来保障你的业务线.当你的客户处在休眠期,或者你的业务处在淡季,你增加的计算 ...

  8. easyui-combogrid匹配查询

    用到easyui-combogrid,数据比较少的情况,可以一页就显示完毕,然后直接下拉选择.但是对于数据量比较大的情况,一页显示全部显然不合适,好在从easyui-combogrid的数据加载方式可 ...

  9. day39---mysql基础三

    1.索引: 字典得目录,便于数据查找. 原理:将列信息存储在其相关的文件,这些信息使用便于检索的方式如B-tree.哈希来存储 索引的分类: 普通所有:name,只能帮助查找 唯一索引:name,帮助 ...

  10. ogre3D学习基础5 -- 阴影与动画

    五.阴影 阴影是渲染一个真实场景的重要组成部分,它可以给场景中的物体提供更加真实的感觉,同时还可以帮助用户更好的了解对象间的空间关系. 启用阴影: 缺省情况下,阴影是关闭的,开启方式如下: 1.建立场 ...