POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】
题目链接: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(旅行商)【节点可多次经过】的更多相关文章
- poj 3311 Hie with the Pie (TSP问题)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4491 Accepted: 2376 ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...
- poj 3311 Hie with the Pie dp+状压
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4671 Accepted: 2471 ...
- POJ 3311 Hie with the Pie 最短路+状压DP
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
- [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
主题连接: id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
- poj 3311 Hie with the Pie (状压dp) (Tsp问题)
这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...
- 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 ...
随机推荐
- java项目(非ssm等框架)下的quartz定时器任务
第一步:引包 要使用Quartz,必须要引入以下这几个包: 1.log4j-1.2.16 2.quartz-2.1.7 3.slf4j-api-1.6.1.jar 4.slf4j-log4j12-1. ...
- ReactNative开发工具有这一篇足矣
ReactNative系列文章: 1.<逻辑性最强的React Native环境搭建与调试> 2.<ReactNative开发工具有这一篇足矣> 正文 React Native ...
- javascript精度问题与调整
一个经典的问题: 0.1+0.2==0.3 答案是:false 因为:0.1+0.2=0.30000000000000004 第一次看到这个结果就是无比惊讶,下巴碰到地上,得深入了解下问题出在哪里,该 ...
- Hibernate 中Hql 查询中间表的用法
案例简述: 项目中存在User 用户表 和 Role 角色表 它们之间是多对多的关系 在User类定义中 使用hibernate注解 //角色列表 @ManyToMany(targetEntity = ...
- Struts 框架 之 Hello World
Struts HelloWorld 第一步 导jar包 commons-fileupload-1.2.2.jar [文件上传相关包] commons-io-2.0.1.jar [输入输 ...
- WinForm中AssemblyInfo.cs文件参数具体讲解
在.NET中有一个配置文件AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些参数,下面是默认的AssemblyInfo.cs文件的内容具体介绍 //是否符合公共语言规 ...
- centos7架设vsftpd服务
网络控制相关命令: systemctl status network 网络状态 systemctl restart network 网络重启 查看网络状态: nmcli connection show ...
- elasticsearch REST API方式批量插入数据
elasticsearch REST API方式批量插入数据 1:ES的服务地址 http://127.0.0.1:9600/_bulk 2:请求的数据体,注意数据的最后一行记得加换行 { &quo ...
- linux中grep命令的用法
作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的. 首先谈一下grep命令的常用格式为:[grep [选项] "模式" [ ...
- Oracle 11g OCM 考试大纲
考试大纲共分9部分. 一.Server Configuration 服务器配置 1 Create the database 创建数据库 2 Determine and set sizing p ...