Travelling

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

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

 
 //2017-08-19
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int INF = 0x3f3f3f3f;
int G[N][N], n, m, pow3[N], dp[N][];//dp[i][S]表示到达节点i,状态为S时的最小费用 //询问节点i在状态S下经过了几次。将状态压缩为一个三进制数。
int query(int i, int S){
return (S/pow3[i])%;
} int main()
{
//预处理3的次方,pow3[n]表示3的n次方。
pow3[] = ;
for(int i = ; i < N; i++)
pow3[i] = pow3[i-]*;
while(scanf("%d%d", &n, &m)!=EOF){
int u, v, w;
memset(G, INF, sizeof(G));
memset(dp, INF, sizeof(dp));
for(int i = ; i < m; i++){
scanf("%d%d%d", &u, &v, &w);
u--; v--;//节点从0到n-1编号。
G[u][v] = G[v][u] = min(G[u][v], w);//去重边
}
//初始化dp,因为每一个点都可以作为起点,所以到达i节点1次的最小费用为0。
for(int i = ; i < n; i++)
dp[i][pow3[i]] = ;
int ans = INF;
for(int S = ; S < pow3[n]; S++){
bool fg = true;
for(int i = ; i < n; i++){
//检查是否每个节点都已经经过
if(query(i, S) == ){
fg = false;
continue;
}
//转移到下一个节点
for(int v = ; v < n; v++){
if(query(v, S) == )
continue;
dp[v][S+pow3[v]] = min(dp[v][S+pow3[v]], dp[i][S]+G[i][v]);
}
}
if(fg){
for(int i = ; i < n; i++)
ans = min(ans, dp[i][S]);
}
}
if(ans == INF)printf("-1\n");
else printf("%d\n", ans);
} return ;
}

HDU3001(KB2-J 状态压缩dp)的更多相关文章

  1. hdu-3001 三进制状态压缩+dp

    用dp来求最短路,虽然效率低,但是状态的概念方便解决最短路问题中的很多限制,也便于压缩以保存更多信息. 本题要求访问全图,且每个节点不能访问两次以上.所以用一个三进制数保存全图的访问状态(3^10,空 ...

  2. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  3. 状态压缩dp相关

    状态压缩dp 状态压缩是设计dp状态的一种方式. 当普通的dp状态维数很多(或者说维数与输入数据有关),但每一维总 量很少是,可以将多维状态压缩为一维来记录. 这种题目最明显的特征就是: 都存在某一给 ...

  4. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  5. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  6. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  7. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  8. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  9. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  10. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

随机推荐

  1. CentOS 设置 oracle 开机自动启动

    CentOS 设置 oracle 开机自动启动 1. [root@localhost ~]# gedit /etc/oratab 文件内容为: # # This file is used by ORA ...

  2. centos7上编译安装mysql5.6

    注意,在做实验室统一关闭防火墙做的,在生产环境需要做防火墙规则的,大家要注意,做的时候尽量都是模仿生产环境的,比如服务一般都在/data/soft下面,尽量避免在/usr/local/下面. 安装编译 ...

  3. Vuex 拾遗

    引入Vuex的目的:为众多的Vue组件提供一个全局管理共享组件状态的控制中心,当一个共享状态改变时,能使调用该共享状态的组件得到更新.并且使用Vuex的API,每个共享状态的改变都能被追踪. 组件如何 ...

  4. 实验1 C语言开发环境使用和数据类型、运算符、表达式

    ♦ 实验结论 PART 1 验证性内容 问题: 1.结尾没有加“:”时回车到下一行的时候再输入下一行的语言首字对齐方式会发生变化,可以对上一行进行检查. (这一点需要在不同软件里面试一下,在机房里的软 ...

  5. opencv实现正交匹配追踪算法OMP

    //dic: 字典矩阵: //signal :待重构信号(一次只能重构一个信号,即一个向量) //min_residual: 最小残差 //sparsity:稀疏度 //coe:重构系数 //atom ...

  6. python中@staticmethod与@classmethod

    @ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...

  7. Selenium自动化测试Python六:持续集成

    持续集成 欢迎阅读WebDriver持续集成讲义.本篇讲义将会重点介绍Selenium WebDriver API的在持续集成中的使用方法,以及使用Jenkins持续集成工具进行自动化测试的设计. 持 ...

  8. .NET平台常用框架

    分布式缓存框架: Microsoft Velocity:微软自家分布式缓存服务框架. Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度. Redis:是一个高性能的K ...

  9. Twitter Bootstrap3小结

    今天有空,小结一下Twitter Bootstrap 3的使用.首先不得不说,Bootstrap是迄今(2014)比较好的WEB设计框架(当然,其它的优秀WEB Framework还有:Foundat ...

  10. Maven启用代理服务器访问

    0.什么叫代理服务器? 代理服务器英文全称是(Proxy Server),其功能就是代理网络用户去取得网络信息.形象的说:它是网络信息的中转站. 代理服务器就好象一个大的Cache,这样就能显著提高浏 ...