昨天想练习一下状态压缩,百度搜索看到有博客讨论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) 旅行商问题的更多相关文章

  1. Hie with the Pie POJ - 3311

    Hie with the Pie POJ - 3311 The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...

  2. 状压dp+floyed(C - Hie with the Pie POJ - 3311 )

    题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...

  3. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  4. poj 3311 Hie with the Pie dp+状压

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 ...

  5. poj 3311 Hie with the Pie (TSP问题)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 ...

  6. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  7. Hie with the Pie(poj3311)

    题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...

  8. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...

  9. Hie with the Pie

    Hie with the Pie poj-3311 题目大意:n+1个点,伪旅行商问题. 注释:n<=10. 想法:咳咳,第一道状压dp,下面我来介绍一下状压dp. 所谓dp,就是动态性决策规划 ...

随机推荐

  1. HIVE文件

    注册表的本地实体文件, 察看位置,以及映射本地文件到注册表中的位置, HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\hivelist 在这里写 ...

  2. .Net串口通讯中的若干问题(C#多串口硬件识别、热插拔、Close方法报错问题、IsOpen的可靠性问题)

    一.需求场景 最近有时间静下心来研究SDK,串口通讯的.要求实现识别cp210x和cp2303驱动的两款硬件,并且2303的优先级高,即有2303识别之,没有再识别210x:要求实现热插拔,拔掉自动断 ...

  3. 【笔记篇】不普及向——莫比乌斯反演学习笔记 && 栗题HAOI2011 Problem B

    Part0 广告(当然没有广告费) P.S. 这篇文章是边学着边用Typora写的...学完了题A了blog也就呼之欲出了~有latex化式子也非常方便...非常建议喜欢Markdown的dalao们 ...

  4. drupal7 smtp+mimemail+mailsystem 实现发送html邮件

    1.下载三个模块 smtp: https://www.drupal.org/project/smtp mimemail: https://www.drupal.org/project/mimemail ...

  5. 推荐一个Java设计模式写的很好的博客

    博客地址:https://quanke.gitbooks.io/design-pattern-java/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E8%AE%BE%E8 ...

  6. Java 虚拟机 - 2.3 HotSpot虚拟机对象

    对象的创建 Step1 类加载检查 当发现一条new指令时,检查: 该指令的参数是否能在常量池中定位到一个类的符号引用: 并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没有,那必须先 ...

  7. vim编码方式设置

    建议vim的_vimrc文件里设置如下的编码方式: set encoding=utf-8 set fileencodings=ucs-bom,utf-8,cp936 set fileencoding= ...

  8. 解析Mybatis入门第二天

    入门第二天 目的:使用Mybatis对数据库中的数据进行简单的操作.例如:增.删.改.查. 前言:同样是使用idea创建一个普通的maven工程(如何创建一个普通的Maven工程可以参考入门第一天的详 ...

  9. Windows 隐藏控制台

    #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"& ...

  10. Divide by Zero 2018 and Codeforces Round #474 (Div. 1 + Div. 2, combined)G - Bandit Blues

    题意:求满足条件的排列,1:从左往右会遇到a个比当前数大的数,(每次遇到更大的数会更换当前数)2.从右往左会遇到b个比当前数大的数. 题解:1-n的排列,n肯定是从左往右和从右往左的最后一个数. 考虑 ...