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. nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument解决

    先附上错误信息: (myblog) root@Dapeng:/home/uwsgi# service nginx status ● nginx.service - A high performance ...

  2. PyCharm 2018.1 软件汉化

    下载汉化包 链接: https://pan.baidu.com/s/1buLFINImW_3cNzP8HsB4cA 密码: fqpu 安装汉化包 找到pycharm安装目录 直接把刚刚下载的汉化包复制 ...

  3. 安全 - 堡垒机 - Jumpserver

    GitHub - jumpserver/Dockerfile: Jumpserver all in one Dockerfile https://github.com/jumpserver/Docke ...

  4. tp5 修改自带success或error跳转模板页面

    tp5 修改自带success或error跳转模板页面 我们在使用tp5或者tp3.2的时候,用的成功或者失败跳转提示页面一般是用框架的.在后续开发过程中,根据实际项目需要,也是可以更改的,在此分享一 ...

  5. PHP之基本目录操作

    一.创建目录 mkdir ($pathname, $mode = 0777, $recursive = false, $context = null) $pathname: 目录路径 $mode : ...

  6. Python中的set

    set_lst = [ ('集合容器不可哈希',), ('集合中的元素必须可哈希',), ('集合是无序的',), ('集合自动去重',), ('增',), ('删',), ('查',), ('集合运 ...

  7. POJ3320 尺取法的正确使用法

    一.前言及题意: 最近一直在找题训练,想要更加系统的补补思维,补补漏洞什么的,以避免被个类似于脑筋急转弯的题目干倒,于是在四处找书,找了红书.蓝书,似乎都有些不尽如人意.这两天看到了日本人的白书,重新 ...

  8. 如何将int转换为datetime?

    $timestamp = 1210003200; $datetime = date('Y-m-d H:i:s', $timestamp); echo "该时间戳代表的时间:", $ ...

  9. SSH进阶之路

    [SSH进阶之路]Hibernate基本原理(一)       在开始学Hibernate之前,一直就有人说:Hibernate并不难,无非是对JDBC进一步封装.一句不难,难道是真的不难还是眼高手低 ...

  10. 启用hyper后无法打开vmware

    十万火急,想办法先让虚拟机能够打开,毕竟经常用. 网上看了无数教程都是让在控制面板中关闭hyper-v,然而并没有用. 找了好久说是不能那样关闭,得用指令.管理员运行powershell,输入下列指令 ...