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 ...
随机推荐
- Windows10 Enterprise版本周年更新问题
安装Windows10的时候,蛋疼选择了企业版本,导致后来周年更新遇到了蛋疼的问题. 首先是无法接收到周年更新的推送.记得首个周年更新的时候,等了大半年都没有.后来使用了易升进行更新,可以检测到,但是 ...
- 【转】JS容器拖拽效果,并通过cookie保存拖拽各容器的所在位置
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- MySQL实例搭建
Q:如何判断一个Linux系统具备安装MySQL的条件? A: 1.Linux网络已经配置完成 ip地址/子网掩码.默认网关.主机名字 /etc/hosts:访问这个数据库的应用的IP地址和主机名字也 ...
- 阿里云服务器linux(cenos)下 jdk、tomcat的安装配置
一.JDK的安装与环境配置 1. 下载jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213315 ...
- 快速查询List中指定的数据
时间:2017/5/15 作者:李国君 题目:快速查询List中指定的数据 背景:当List中保存了大量的数据时,用传统的方法去遍历指定的数据肯定会效率低下,有一个方法就是类似于数据库查询那样,根据索 ...
- async 函数
同步 console.log(1); console.log(2); console.log(3); console.log(4); //异步 ajax 文件读取io操作 console.log(1) ...
- nyoj_83:迷宫寻宝(二)(计算几何)
题目链接 枚举所有墙的2n个端点与宝物的位置作为一条线段(墙的端点必定与边界重合), 求出与之相交的最少线段数(判断线段相交时用跨立实验的方法),+1即为结果. #include<bits/st ...
- Objective-C plist文件与KVC 的使用
plist文件是以类似xml形式构造数据,下面我们直接在xcode中创建完成一个plist文件, File-New-File-属性列表 我们可以选择存储类型.这里我构造一组数据,数据中的每个元素都是一 ...
- golang windows 安装方法
编译器下载链接:https://golang.org/dl/ 默认安装到C盘,不用修改. 添加环境变量: 配置环境变量: 注:C:\mygo\bin 配置这个后,则可以直接在 Dos ...
- 【php】php 连接数据库
$mysql_server_name=""; //数据库服务器名称 $mysql_username=""; // 连接数据库用户名 $mysql_passwor ...