Hie with the Pie (POJ 3311) 旅行商问题
昨天想练习一下状态压缩,百度搜索看到有博客讨论POJ 3311,一看就是简单的旅行商问题,于是快速上手写了状态压缩,死活样例都没过。。。
画图模拟一遍原来多个城市可以重复走,然后就放弃思考了。。。
刚刚把这个无聊的问题解决了,简单的Floyd+状压。
所谓Floyd算法,我在暑训的博客里提过,复杂度O(n3),但当时数据量太大不适合使用,这里刚好派上了用场。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; const int INF = 0x3f3f3f3f;
int dis[][], n;
int dp[<<][]; // dp[S][u] 从u出发经过S的剩下城市走到0的最短时间 int dfs(int S, int u)
{
if(dp[S][u]>)
return dp[S][u]; if(S==(<<(n+))- && u==)
return dp[S][u] = ; int res = INF;
for(int v=;v<=n;v++)
{
if(v!=u && !((S>>v)&))
res = min(res, dfs(S|(<<v), v)+dis[u][v]); }
return dp[S][u] = res;
}
int main()
{
while(cin>>n)
{
if(n==) break; for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>dis[i][j]; // Floyd算法,得到任意两个城市之间的最短距离
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
memset(dp, -, sizeof(dp));
cout<<dfs(, )<<endl;
}
return ;
}
交一发直接AC了感觉索然无味啊。。。还是要多找点难题刷刷 0.0
Hie with the Pie (POJ 3311) 旅行商问题的更多相关文章
- Hie with the Pie POJ - 3311
Hie with the Pie POJ - 3311 The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...
- 状压dp+floyed(C - Hie with the Pie POJ - 3311 )
题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- poj 3311 Hie with the Pie dp+状压
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4671 Accepted: 2471 ...
- poj 3311 Hie with the Pie (TSP问题)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4491 Accepted: 2376 ...
- POJ 3311 Hie with the Pie 最短路+状压DP
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
- Hie with the Pie(poj3311)
题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...
- POJ3311 Hie with the Pie 【状压dp/TSP问题】
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total ...
- Hie with the Pie
Hie with the Pie poj-3311 题目大意:n+1个点,伪旅行商问题. 注释:n<=10. 想法:咳咳,第一道状压dp,下面我来介绍一下状压dp. 所谓dp,就是动态性决策规划 ...
随机推荐
- 详解redis服务
http://mp.weixin.qq.com/s?__biz=MzIyMDA1MzgyNw==&mid=2651968327&idx=1&sn=6e6cb01d334d7ae ...
- Java中逗号运算符的使用
今天看到一道面试题,题目如下: 题目解释: 上面有一个类,有四个成员属性,以及两个构造方法(一个四个参数,一个两个参数),题目给出四个选项,问哪些选项可以满足在在四个参数的构造方法中完成x=a,y=b ...
- C++: string copy-on-write
https://blog.csdn.net/haoel/article/details/24058 https://www.cnblogs.com/promise6522/archive/2012/0 ...
- Python re标准库
re模块包含对正则表达式的支持. 一.什么是正则表达式 正则表达式是可以匹配文本片段的模式.最简单的正则表达是就是普通字符串,可以匹配其自身.你可以用这种匹配行为搜索文本中的模式,或者用计算后的值替换 ...
- 在vue中使用高德地图vue-amap
1.安装 vue-amap我安装指定版本0.5.10的版本 npm i --save vue-amap@0.5.10 2.main.js中的配置 key申请地址教程:https://lbs.amap. ...
- thinkphp5+GatewayWorker+Workerman
项目地址 ttps://www.workerman.net/workerman-chat thinkphp5+GatewayWorker+Workerman聊天室,可以多人聊天,指定某个人进行聊天, ...
- Java 基础 - final vs static
总结 共同点,都可以修饰类,方法,属性.而不同点: static 含义:表示静态或全局,被修饰的属性和方法属于类,可以用类名.静态属性 / 方法名 访问 static 方法:只能被static方法覆盖 ...
- jQuery FormData使用方法
FormData的主要用途 将form表单元素的name与value进行组合,实现表单数据的序列化,从而减少表单元素的拼接,提高工作效率. 异步上传文件 注:FormData 对象的字段类型可以是 B ...
- 用VC生成 IDispatch 包装类
1.创建包装类:View->ClassWizard->Add Class->Add Class From ActiveX Control Wizard 2 .选中Registry 3 ...
- C语言进阶学习第一章
1.在C语言里面使用scanf给某个变量赋值时候,如果成功返回1,失败返回0:测试代码如下: /***假如在键盘输入的不是整形数据,则输出0,否则输出1***/ void main() { int a ...