题目链接: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. OSX MacVim + vim-lldb配置和使用心得

    Mac里面默认的编译器是clang/clang++ 所以debugger就选择了lldb 想搭配MacVim一起使用,于是就找到了vim-lldb这个插件,相当强大   这个插件支持Vundle,所以 ...

  2. Java基础(6)- 面向对象解析

    java面向对象 对象 知识点 java 的方法参数是按值调用,是参数的一份拷贝 封装 使用private将 属性值/方法 隐藏,外部只能调用 get,set方法/非private 的接口 获取 重载 ...

  3. JavaScript 定义 类

    JavaScript 定义 类 一 构建类的原则 构造函数 等于 原型的constructor //构造函数 function Hero(name,skill){ this.name = name; ...

  4. Hibernate错误:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

    报错:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execu ...

  5. tcp/ip通信传输流

    利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信,发送端从应用层往下走,接收端则往应用层方向走. 我们用HTTP进行举例 客户端在应用层发出想要看到某个web页面的http请求.HT ...

  6. vijos1101题解

    题目: 研究表明,这种传染病的传播具有两种很特殊的性质: 第一是它的传播途径是树型的,一个人X只可能被某个特定的人Y感染,只要Y不 得病,或者是XY之间的传播途径被切断,则X就不会得病. 第二是,这种 ...

  7. Educational Codeforces Round 21 A-E题题解

    A题      ............太水就不说了,贴下代码 #include<string> #include<iostream> #include<cstring& ...

  8. 多线程下System.Security.Cryptography.Aes CreateDecryptor报“Safe handle has been closed”的解决方案

    因为系统需要对一些核心数据进行预加载以保证查询速度. 所以在application_start 事件中启用了后台线程对相关的数据进行加载并解密(为了保证解密的效率,将AES对像做了静态对像来保存:pr ...

  9. JAXP Dom 案例 对xml文件进行增加 查找 删除

    利用 JAXP 对 XML文件 的处理,把xml当做一个数据库来对待

  10. php接口加密

    在实际开发中PHP接口开发是PHP常见的技术,可以通过接口进行一些操作,可以访问接口获取数据,给app提供数据接口等,但是如果接口没有加密解密的过程,很容易发生盗用和不安全的操作. 下边简单介绍一下A ...