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. 一文搞懂Java环境,轻松实现Hello World!

    在上篇文章中,我们介绍了Java自学大概的路线.然而纸上得来终觉浅,今天我们教大家写第一个java demo.(ps:什么是demo?Demo的中文含意为“示范",Demo源码可以理解为某种 ...

  2. Hello AS400-Cobol

    因工作转换,进入金融行业,需要接触AS400-Cobol 在C#和Java的技术栈中再增加一笔,技术只是工具,无关乎新旧,获得编程思想和经验是无价的.

  3. cStringIO 实现指定大小的字符串缓存

    StringIO经常被用来作为字符串的缓存,以下实现无论写入多少字符串,总能返回一个指定大小的缓存 from cStringIO import StringIO class CustomStringI ...

  4. ES代码总结2

    本文部分转载于: http://www.cnblogs.com/luxiaoxun/p/4869509.html ElasticSearch的基本用法与集群搭建  一.简介 ElasticSearch ...

  5. (转)Python3之pickle模块

    原文:https://www.cnblogs.com/wang-yc/p/5616579.html https://www.cnblogs.com/yuanzhaoyi/p/6093362.html- ...

  6. 一眼看穿flatMap和map的区别

    背景 map和flatmap,从字面意思或者官网介绍,可能会给一些人在理解上造成困扰[包括本人],所以今天专门花时间来分析,现整理如下: 首先做一下名词解释---------------------- ...

  7. Redis学习系列一Linux环境搭建

    1.简介 Redis是互联网技术架构中在存储系统中用的最广泛的中间件,是中高级后端工程师技术面试中面试官最喜欢问的工程技能之一.所以Redis是.Net技术开发必须掌握的技能之一.所以通过这个系列的随 ...

  8. nginx配置文件 nginx.conf 说明

    #user nobody; #开启进程数 <=CPU数  worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #error_log ...

  9. .net core2 mvc项目中,加入RazorPages页面

    2017.08.22 试验结果: 1.手工添加/Pages文件夹 2.复制/Views/_ViewImports.cshtml到/Pages/_ViewImports.cshtml  2.1 修改@u ...

  10. 获取Javascript 滚动条距离顶部的距离(兼容IE6+,火狐,谷歌,其它没测)

    document.body.scrollTop || document.documentElement.scrollTop