题目链接:http://poj.org/problem?id=3311

题意:

  你在0号点(pizza店),要往1到n号节点送pizza。

  每个节点可以重复经过。

  给你一个(n+1)*(n+1)的邻接矩阵,表示各点之间距离。

  问你送完所有pizza再返回店里的最短路程。

题解:

  与传统TSP相比,唯一变化的条件是每个节点可以经过多次。

  所以也就是转移的时候不用再判断要去的节点j是否去过。

  先floyd预处理出两点之间最短路。然后把(!((state>>j)&1))去掉,套TSP模板就好啦。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#define MAX_N 15
#define MAX_S (1<<13)
#define INF 10000000 using namespace std; int n;
int ans;
int dp[MAX_S][MAX_N];
int dis[MAX_N][MAX_N]; void floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i!=j && j!=k && i!=k)
{
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
}
} void read()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>dis[i][j];
}
}
} void solve()
{
floyd();
memset(dp,-,sizeof(dp));
dp[][]=;
for(int state=;state<(<<(n+));state++)
{
for(int i=;i<=n;i++)
{
if(dp[state][i]!=-)
{
for(int j=;j<=n;j++)
{
if(dp[state|(<<j)][j]==- || dp[state|(<<j)][j]>dp[state][i]+dis[i][j])
{
dp[state|(<<j)][j]=dp[state][i]+dis[i][j];
}
}
}
}
}
ans=INF;
for(int i=;i<=n;i++)
{
if(dp[(<<(n+))-][i]!=-)
{
ans=min(ans,dp[(<<(n+))-][i]+dis[i][]);
}
}
} void print()
{
cout<<ans<<endl;
} int main()
{
while(cin>>n)
{
if(n==) break;
read();
solve();
print();
}
}

POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】的更多相关文章

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

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

  2. poj 3311 Hie with the Pie

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

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

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

  4. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

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

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

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

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

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

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

  8. poj 3311 Hie with the Pie (状压dp) (Tsp问题)

    这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...

  9. POJ 3311 Hie with the Pie 兼 Codevs 2800 送外卖(动态规划->TSP问题)

    Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possi ...

随机推荐

  1. python 三级菜单 while循环三次,湖北省市-县-街道的选择,3个while的循环 -day2

    python编写一个三级while的循环菜单 1.定义字典,字典里面嵌套字典,内嵌字典的值为列表. 思路: 湖北省的市:字典中的定义3个字典,用于存储{序列-键:市名} shiqu_dir = {} ...

  2. Python对象类型及其运算

    Python对象类型及其运算 基本要点: 程序中储存的所有数据都是对象(可变对象:值可以修改 不可变对象:值不可修改) 每个对象都有一个身份.一个类型.一个值 例: >>> a1 = ...

  3. 最小的 Velocity 教程

    工作以后,我越来越能体会到80/20法则的强大. 这是一个不可否认的事实,常用 20% 的技术可以解决工作中 80% 的场景. 所以我希望能介绍给你 Velocity 技术 20%,帮助你胜任 80% ...

  4. Oracle查询多行数据合并成一行数据

    例如: select base_id, translate (ltrim (text1, '/'), '*/', '*,') xmmc,translate (ltrim (text2, '/'), ' ...

  5. 2-用EasyNetQ连接RabbitMQ(黄亮翻译)

    如果你连接过关系数据库,例如SQL Server.你会发现EasyNetQ处理connections有点奇怪.和关系数据库通讯一直都是通过client开始的.Client 打开一个连接, 发出一个SQ ...

  6. Python如何实现单步调试

    遇到大型python项目,如何定位问题和监控程序的运行状态是一个程序员必须掌握的技能,今天小编为你带来python程序的单步调试方法,方便易用,简单易记! 首先你需要在所调试程序的开头中:import ...

  7. Javascript百学不厌-递归

    虽然偶尔也用过,但是从来没具体来整理过 普通递归: function fac(n) { ) ; ); } fac() 这是个阶乘.但是占用内存,因为: fac(5) (5*fac(4)) (5*(4* ...

  8. Phpcms整理

    一.先去官网下载一个pc(http://www.phpcms.cn/)进行安装 把下载的pc包放在服务器www目录下: 在地址栏访问localhost/project/install/install. ...

  9. .Net中关于相等的问题

    在.Net框架中,如果您查看所有类型的的基类:System.Object类,将找到如下4个与相等判断的方法: static Equals() virtual Equals() static Refer ...

  10. 【mysql】常用操作

    2.mysql service mysql status mysql --version mysql -h 服务器主机地址 -u 用户名 -p 用户密码 exit  退出 mysql -h 主机名 - ...