题意: 给i到j花费的地图 1到n编号   一个人要从1遍历n个城市后回到1

求最小的花费(可以重复走)

分析

http://www.cnblogs.com/Empress/p/4039240.html

TSP

因为可以重复走 所以先floyd一下求最短路

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <climits>
  5. #include <cctype>
  6. #include <cmath>
  7. #include <string>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <iomanip>
  12. using namespace std;
  13. #include <queue>
  14. #include <stack>
  15. #include <vector>
  16. #include <deque>
  17. #include <set>
  18. #include <map>
  19. typedef long long LL;
  20. typedef long double LD;
  21. #define pi acos(-1.0)
  22. #define lson l, m, rt<<1
  23. #define rson m+1, r, rt<<1|1
  24. typedef pair<int, int> PI;
  25. typedef pair<int, PI> PP;
  26. #ifdef _WIN32
  27. #define LLD "%I64d"
  28. #else
  29. #define LLD "%lld"
  30. #endif
  31. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  32. //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
  33. //inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
  34. //inline void print(LL x){printf(LLD, x);puts("");}
  35. //inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}
  36.  
  37. int dp[<<][], mp[][];
  38. int n;
  39. void floyd()
  40. {
  41. for(int k=;k<n;k++)
  42. for(int i=;i<n;i++)
  43. for(int j=;j<n;j++)
  44. mp[i][j]=min(mp[i][j], mp[i][k]+mp[k][j]);
  45. }
  46. int main()
  47. {
  48. #ifndef ONLINE_JUDGE
  49. freopen("in.txt", "r", stdin);
  50. freopen("out.txt", "w", stdout);
  51. #endif
  52. while(~scanf("%d", &n) && n)
  53. {
  54. n++;
  55. for(int i=;i<n;i++)
  56. for(int j=;j<n;j++)
  57. scanf("%d", &mp[i][j]);
  58. floyd();
  59. memset(dp, , sizeof(dp));
  60. dp[(<<n)-][]=;
  61. for(int s=(<<n)-;s>=;s--)
  62. for(int v=;v<n;v++)
  63. for(int u=;u<n;u++)
  64. if(!(s>> u & ))
  65. dp[s][v]=min(dp[s][v], dp[s | <<u][u]+mp[v][u]);
  66. printf("%d\n", dp[][]);
  67. }
  68. return ;
  69. }

POJ 3311

[TSP+floyd]POJ3311 Hie with the Pie的更多相关文章

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

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

  2. poj3311 Hie with the Pie (状态压缩dp,旅行商)

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

  3. [poj3311]Hie with the Pie(Floyd+状态压缩DP)

    题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 ...

  4. POJ3311 Hie with the Pie(状压DP,Tsp)

    本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费 ...

  5. 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]

    题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...

  6. POJ3311 Hie with the Pie

    The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortu ...

  7. Hie with the Pie(poj3311)

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

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

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

  9. Hie with the Pie

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

随机推荐

  1. 使用blktrace排查iowait cpu高的问题

    本文转自这里,blktrace在这种情况下的使用方法值得借鉴学习. ------------------------------------------------------------------ ...

  2. Android richtext

    在项目开发过程中经常会遇到很多需要显示不同样式的,不同风格的文本信息:对此可以使用多个TextView来分别设置自已想要的样式以满足需求,但是使用多个TextView的方式不太好:使用多个TextVi ...

  3. SQL Server(基本) 关键字的使用 一

    一, 基础关键字 -- 使用介绍 1,select 的使用(select 结果集) SELECT 列名称 FROM 表名称 以及: (*)是选取所有列的快捷方式. SELECT * FROM 表名称 ...

  4. C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55

    //C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55 using System; using System.Collections.Generic; using S ...

  5. iOS开发——极光推送

    1.到极光官网 https://www.jpush.cn/ 下载极光推送SDK. 具体如何集成最好参考官网的文档,以及一些失败的原因.文档非常详细,我也是参考集成的. 2.到极光推送官网注册自己的应用 ...

  6. C#基础总复习01

    马上就快毕业了,准备把这几个月所学到的知识梳理一下,这儿所写的都是一些C#中最基础的东西(大牛不要笑话我,这也是我记录的一些笔记等等),希望能帮到一些正在学习这方面的知识的人,如果有写的不对的地方,望 ...

  7. 【HeadFirst设计模式】12.复合模式

    定义: 复合模式结合两个或以上的模式,组成一个解决方案,解决一再发生的一般性问题. 要点: MVC模式是复合模式,结合了观察者模式.策略模式和组合模式. 模型使用了观察者模式,以便观察者更新,同时保存 ...

  8. GCC编译器入门

    GCC(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分.GCC原本作为GNU操作 ...

  9. java oop

    /** 多层嵌套内部类, 调用时要层层往下调用 格式: 外部类.内部类1.内部类2 对象名 = new 外部类().new 内部类1().new 内部类2(); 对象名.属性/方法名(); */ cl ...

  10. "严格模式" use strict 详解

    一.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...