设dp状态为dp[i][j]为当前访问过的结点状态为i且当前停留点为j时的最短路径。用二进制存存储访问过的状态,访问过为1,否则为0。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. const int inf=(1<<30);
  8. int map[12][12];
  9. int dp[1<<11][12],n;
  10.  
  11. struct Status{
  12. int i,j,v;
  13. Status(){}
  14. Status(int ii,int jj,int vv){i=ii,j=jj,v=vv;}
  15. }que[(1<<11)*12];
  16. int head,tail;
  17.  
  18. void slove(){
  19. while(head<tail){
  20. Status tmp=que[head++];
  21. if(tmp.v>dp[tmp.i][tmp.j]) continue;
  22. for(int j=0;j<=n;j++){
  23. int st=(1<<j);
  24. int tst=tmp.i|st;
  25. if(dp[tst][j]>tmp.v+map[tmp.j][j]){
  26. dp[tst][j]=tmp.v+map[tmp.j][j];
  27. que[tail++]=Status(tst,j,dp[tst][j]);
  28. }
  29. }
  30. }
  31. printf("%d\n",dp[(1<<n+1)-1][0]);
  32. }
  33.  
  34. int main(){
  35. while(scanf("%d",&n),n){
  36. for(int i=0;i<=n;i++){
  37. for(int j=0;j<=n;j++)
  38. scanf("%d",&map[i][j]);
  39. }
  40. for(int i=0;i<(1<<(n+1));i++){
  41. for(int j=0;j<=n;j++)
  42. dp[i][j]=inf;
  43. }
  44. dp[1][0]=0;
  45. head=tail=0;
  46. que[tail++]=Status(1,0,0);
  47. slove();
  48. }
  49. return 0;
  50. }

  

POJ 3311的更多相关文章

  1. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  2. Hie with the Pie POJ - 3311

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

  3. Hie with the Pie (POJ 3311) 旅行商问题

    昨天想练习一下状态压缩,百度搜索看到有博客讨论POJ 3311,一看就是简单的旅行商问题,于是快速上手写了状态压缩,死活样例都没过... 画图模拟一遍原来多个城市可以重复走,然后就放弃思考了... 刚 ...

  4. poj 3311(floyd+状态压缩)

    题目链接:http://poj.org/problem?id=3311 思路:Floyd + 状态压缩DP  题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且 ...

  5. poj 3311 Hie with the Pie

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

  6. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

  7. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  8. [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法

    主题连接:  id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...

  9. POJ 3311 Hie with the Pie floyd+状压DP

    链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...

  10. POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】

    题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...

随机推荐

  1. $P2299 Mzc和体委的争夺战$

    \(problem\) #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef long lon ...

  2. JavaScript--控制类名(className 属性)

    className 属性设置或返回元素的class 属性. 语法: object.className = classname 作用: 1.获取元素的class 属性 2. 为网页内的某个元素指定一个c ...

  3. 解决gradle project refresh failed: protocol family unavailable问题的几种方法

    Android Studio从版本1.5更新到2.1之后,打开Android Studio一直提示: gradle project refresh failed: protocol family un ...

  4. [转]Linux下paste命令详解

    转自:http://blog.csdn.net/andy572633/article/details/7214126 paste单词意思是粘贴.该命令主要用来将多个文件的内容合并,与cut命令完成的功 ...

  5. 【转】Linux 之 数据流重定向

    转自:http://www.linuxidc.com/Linux/2012-09/69764.htm linux在你登入时,便将默认的标准输入.标准输出.标准错误输出安排成你的终端.I/O重定向就是你 ...

  6. SQL Sever语言 事务

    事务:保障流程的完整执行保证程序某些程序在运行时同时成功同时失败,保证程序的安全性 begin tran --在流程开始的位置加 --此处写SQL语句 if @@error>0 --ERRORS ...

  7. 【RTTI】java Class类详解

    RTTI (Run-Time Type Information)运行时类信息 Java的Class类是java反射机制的基础,通过Class类我们可以获得关于一个类的相关信息,下面我们来了解一下有关j ...

  8. [ NOIP 2008 ] TG

    \(\\\) \(\#A\) \(Word\) 给出一个长为\(N\)的小写字母串,判断出现所有字母中最多出现次数减最少出现次数得到的答案是否是质数. \(N\in [1,100]\) 直接按题意开桶 ...

  9. fcc html5 css 练习3

    行内样式看起来是这样的 <h1 style="color: green"> .pink-text { color: pink !important; }         ...

  10. html 报告页面 v1.2 批量数据生成表格

    html 报告页面 v1.2 批量数据生成表格 上代码: <!DOCTYPE html> <html lang="en"> <head> < ...