昨天想练习一下状态压缩,百度搜索看到有博客讨论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. 关于strtok函数

    函数原型: char *strtok(char * strToken, const char *strDelimit) 参数说明: strToken:源字符串,即待分割的串 strDelimit:st ...

  2. 【POJ】2240 Arbitrage

    题目链接:http://poj.org/problem?id=2240 题意:n种国家的货币,m个换算汇率.问你能不能赚钱. eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美 ...

  3. Docker学习のDocker镜像

    一.列出镜像 命令:docker images [optsions] [repositort] -a 标识列出所有 -f  写过滤条件 --no-trunc  不截断id -q 只显示唯一id rep ...

  4. iOS开发系列-iOS布局相关

    LayoutSubViews 需要在某个View调整子视图的位置时,可以重写. 以下情况会出发LayoutSubViews方法的调用 init初始化不会触发layoutSubviews,但是是用ini ...

  5. Hive应用实例:WordCount

  6. Leetcode976. Largest Perimeter Triangle三角形的最大周长

    给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. 如果不能形成任何面积不为零的三角形,返回 0. 示例 1: 输入:[2,1,2] 输出:5 示例 2 ...

  7. 360自动抢票还不够,几行js代码设置无人值守

    360就是牛逼哄哄的...... 但是最近在使用360浏览器抢票的时候还是发现了一些体验不好的地方,比如搞着搞着就退出了登录,有时候能帮你自动登录进去,但是自动登录之后又不会帮你自动开始抢.然后验证码 ...

  8. AndroidStudio WiFi调试插件

    前言 此篇博客也是Android studio插件篇的一部分,后续有时间我会介绍更多AndroidStudio的插件方便开发. Android设备用WiFi调试在以前一般是通过adb连接的,但是这样的 ...

  9. Image 释放

    你是用 Image.FromFile取的图片吧,这个方法会锁定图片文件. 有几种方法解决:一:System.Drawing.Image img = System.Drawing.Image.FromF ...

  10. C/C++ 公有函数无法返回私有的类对象解决方案

    { 能出这种错的说明还需要提升C++,增强对类的理解 解决方案:把你的私有的对象的私有的拷贝构造或者同类赋值改为公开的 }