Travelling(spfa+状态压缩dp)
2015-09-28 08:22
337 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001
Travelling
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5295 Accepted Submission(s): 1718
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Output the minimum fee that he should pay,or -1 if he can't find such a route.
[align=left]Sample Input[/align]
2 1
1 2 100
3 2
1 2 40
2 3 50
3 3
1 2 3
1 3 4
2 3 10
[align=left]Sample Output[/align]
100
90
7
[align=left]Source[/align]
2009 Multi-University Training Contest 11 - Host by HRBEU
题意:一个地图可以从任意一个地点出发,到达遍历所有的点,每个点最多可以被访问2次,问这样遍历所有的点的最小边权和是多少。
题解:观察这个题的数据范围是10,而且要求每个点有3个状态:未被访问,被访问1次,被访问2次。是一个类似于汉密顿的问题(汉密顿问题是每个点经过且只经过一次)
考虑用状态压缩dp来写,但是一般的状态压缩是用0,1表示访问和未访问两个状态,所以用一个二进制数来表示这个地图的某个状态。而这个题是一个点有三个状态,未被访问,被访问一次,被访问2次。所以自然的想到用一个三进制数来表示,集合的运算完全类比于二进制的情况。
参见/article/5243506.html 的spfa思路
代码:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> using namespace std; #define N 11 #define INF 0x7f7f7f7f//注意memset里的0x7f的值在int中是0x7f7f7f7f int mp ; bool vis[177777] ; int dp[177777] ; queue<pair<int,int> > q; bool ch(int s, int n) { for(int i = 0; i < n; i++) { if(s%3 == 0) return false; s /= 3; } return true; }//检验最后的这个状态中是否是每个点都访问过 int main() { int n , m ; while(~scanf("%d%d",&n,&m)) { memset(vis,0,sizeof(vis)); memset(dp,0x7f,sizeof(dp)); memset(mp,0x7f,sizeof(mp)); for(int i =0 ;i < n ;i++) mp[i][i] = 0; dp[0][0] = 0; vis[0][0] = 1; q.push(make_pair(0,0)); for(int i = 0; i < n; i++){ dp[(int)(pow(3, i))][i] = 0; q.push(make_pair(pow(3, i), i)); } int u, v , d; for(int i =0 ; i < m ; i++) { scanf("%d%d%d",&u,&v,&d); u--,v--; mp[u][v] = mp[v][u] = min(mp[u][v],d); } int tm = pow(3,n)-1; while(!q.empty()) { int s = q.front().first; int u = q.front().second; q.pop(); vis[s][u] = 0; int cur = s, ss; for(int i = 0 ;i < n ; i++) { int bt = cur % 3; cur /= 3;//因为要考虑到没有用的数,及这个位置已经是2了就要在这个位置的下一位考虑了 if(bt < 2) { ss = s + pow(3, i); if(dp[ss][i]>dp[s][u]+mp[i][u]){ dp[ss][i] = dp[s][u] + mp[i][u]; if(vis[ss][i] == 0){ vis[ss][i] = 1; q.push(make_pair(ss,i)); } } } } } //for(int i = 0; i < pow(3, n); i++) // for(int j = 0; j < n; j++) // printf("%d %d : %d\n", i, j, dp[i][j]); int ans = INF; for(int s = 0; s <= tm; s++) for(int i = 0 ;i < n ;i++) if(ch(s, n)) ans = min(ans,dp[s][i]); if(ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- vs2010中的错误错误C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型和Unicode字符集和多字节字符集关系
- 使用POI插件,提取导出excel的工具类
- jQuery.extend函数源码详解
- 第七届湘计算机程序设计竞赛的学生 报道称,数字游戏
- DOM中文本和标记的插入
- BZOJ 2743 [HEOI2012]采花 离线+线段树
- web:2、httpd的特性和配置详解
- HashMap和HashTable源码学习笔记
- 修改远程桌面端口让win7更安全
- poj1006-Biorhythms(中国剩余定理)
- AspNet MVC4 教育-28:Asp.Net MVC4 Ajax技术部门四舍五入余速Demo
- OpenCV中feature2D学习——ORB和BruteForceMatcher
- 9-25考试第三题解题报告(还是写了指针翻译神马的都是浮云感觉好久不说话连话都不会说了233333333QVO)
- 基于XBee进行ZigBee组网(二)——ZigBee网络与XCTU的使用
- [codevs2467]午餐 排序+dp+部分贪心
- 个人简历制作工具V1.09
- Win10正式版内置应用问题多 希望TH2能重点修复
- 图片路径问题
- 机房收费系统常见问题
- OpenCV原则解读HAAR+Adaboost