POJ 3311 Hie with the Pie (状压DP)
题意:
每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少?
思路:
同HDU 5418 VICTOR AND WORLD (可重复走的TSP问题,状压DP)这道题几乎一模一样。
1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <vector> 9 #include <iostream> 10 #define pii pair<int,int> 11 #define INF 0x3f3f3f3f 12 #define LL long long 13 #define ULL unsigned long long 14 using namespace std; 15 const double PI = acos(-1.0); 16 const int N=12; 17 18 int g[N][N], dp[1<<11][N]; 19 void floyd(int n) 20 { 21 for(int k=1; k<=n; k++) 22 for(int i=1; i<=n; i++) 23 for(int j=1; j<=n; j++) 24 g[i][j]=min( g[i][j], g[i][k]+g[k][j]); 25 26 } 27 28 int cal( int n ) 29 { 30 floyd(n); 31 memset(dp, 0x7f, sizeof(dp)); 32 dp[1][1]=0; 33 34 for(int s=1; s<(1<<n); s++) //枚举状态 35 { 36 for(int i=1; i<=n; i++) //枚举中间点:必须已访问 37 { 38 if( s&(1<<i-1) ) 39 { 40 for(int j=1; j<=n; j++) //枚举终点 41 { 42 int &q=dp[s|(1<<(j-1))][j]; 43 q=min(q, dp[s][i]+g[i][j] ); 44 } 45 } 46 } 47 } 48 int ans=dp[(1<<n)-1][1]; 49 for(int i=2; i<=n; i++) 50 ans=min(ans, dp[(1<<n)-1][i] +g[i][1]); 51 return ans; //只有1个点的情况 52 } 53 54 int main() 55 { 56 //freopen("input.txt","r",stdin); 57 int n; 58 while(scanf("%d",&n), n) 59 { 60 memset(g,0x3f,sizeof(g)); 61 ++n; 62 for(int i=1; i<=n; i++) 63 for(int j=1; j<=n; j++) 64 scanf("%d",&g[i][j]); 65 66 printf("%d\n",cal(n)); 67 } 68 return 0; 69 }
AC代码
转载于:https://www.cnblogs.com/xcw0754/p/4858888.html
POJ 3311 Hie with the Pie (状压DP)相关推荐
- Hie with the Pie (状压 DP)
从 0 点出发,每个点可以走多次.最后再回到 0 点,问最小的距离是多少. 因为是从 0 点出发,所以每次更新的时候,我们要保证 0 这个位置一定要是 1 . for 循环的时候 i = i | 1 ...
- [POJ 3311] Hie with the Pie
这是状压DP中常见的一类问题(TSP问题)(废话) 题目大意: 给你一个n+1个点的有向完全图,现在要你从0号节点出发走过1-n号点至少一次,再返回0号点的最少时间,1<=n<=10 题意 ...
- POJ - 3311 Hie with the Pie
题目: The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unf ...
- POJ 1321 棋盘问题(DFS 状压DP)
用DFS写当然很简单了,8!的复杂度,16MS搞定. 在Discuss里看到有同学用状态压缩DP来写,就学习了一下,果然很精妙呀. 状态转移分两种,当前行不加棋子,和加棋子.dp[i][j]中,i代表 ...
- POJ 3311 Hie with the Pie (flyod + DFS枚举)
先用floyd算法求出 2点间的最短距离,然后再用深搜枚举即可. #include<cstdio> #include<cstring> #include<iostream ...
- POJ 2288 Islands and Bridges(状压dp)
http://poj.org/problem?id=2288 题意: 有n个岛屿,每个岛屿有一个权值V,一条哈密顿路径C1,C2,...Cn的值为3部分之和: 第1部分,将路径中每个岛屿的权值累加起来 ...
- poj 2288 Islands and Bridges (状压dp+Tsp问题)
这道题千辛万苦啊! 这道题要涉及到当前点和前面两个点,那就设dp[state][i][j]为当前状态为state,当前点为i,前一个点为j 这个状态表示和之前做炮兵那题很像,就是涉及到三个点时,就多设 ...
- Hie with the Pie(Floyd 状压DP)
POJ 3311 Hie with the Pie 题目大意 一个披萨店要请司机来送披萨,这家店最多接受10个订单,由于预算的问题,只能雇佣一名司机,要求用最短的时间,从披萨店出发送完最后再回到披萨店 ...
- 百练4124:海贼王之伟大航路(状压DP)
题目来源:http://bailian.openjudge.cn/practice/4124/ 4124:海贼王之伟大航路 总时间限制: 1000ms 内存限制: 65536kB 描述 " ...
- 【POJ 3311】Hie with the Pie(状压DP)
Hie with the Pie 题目链接:POJ 3311 题目大意 给你 n+1 个点,其中 0 号点是特殊点. 然后两个点之间都有路径可以走. 然后你要从特殊点出发走过所有点回到特殊点,问你最短 ...
最新文章
- 送你一份使用k近邻算法实现回归的实用指南(附代码、链接)
- asp.net的三层架构图
- oracle客户端没有装exp,oracle 10G 客户端安装后,尝试 exp 命令报错, 求解
- 击败安全系统系列文章之突破卡巴6
- 访问域名不走dns服务问题排查,报错could not resolve host
- 前端中全部盒子靠左对齐_前端面试一百问之弹性盒子中 flex: 0 1 auto 表示什么意思...
- 使用技巧_Lodash 使用技巧
- ultraedit26 运行的是试用模式_如何并行运行你的自动化测试?
- QGLViewer 编译安装步骤
- jquery.text()和.html()的原理
- mysql 本地热备_MySQL 数据库热备的操作
- 使用ComponentOne C1WebGrid控件
- RPG游戏-NPC系统
- 如何使用水经注下载地图
- C#获取电脑硬盘序列号
- 创新设计思维自学报告
- 微信SVG使用指南 01
- 开发跨设备的鸿蒙(HarmonyOS) App
- 脚踏实地,不要飘忽不定
- [翻译] Dremel made simple with Parquet
热门文章
- 适配器模式在 Java 日志中的应用
- 堆的应用之优先级队列
- kafka内置分区及自定义分区
- 停止预览时调用Camera.release(), 出现Method called after release()异常问题原因及解决办法...
- vmware esxi 4.0 上安装postfix,mailx发送邮件
- 怎么引导2岁孩子洗手问题
- 巧妙检查WinXP系统漏洞
- 09年最值得期待7大IT收购:思科收购VMware
- spark Transformations算子
- [Swift]LeetCode229. 求众数 II | Majority Element II