主题连接:



id=3311">http://poj.org/problem?id=3311

题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解

思路:用二进制数来表示訪问过的城市集合。f[{S}][j]=已经訪问过的城市集合为S,訪问了j个城市。所需的最少花费。

这里提一下二进制数表示集合的方法(这里最好还是设集合中最多有n个元素):

假设集合S中最多会出现n个元素,则用长度为n的二进制数来表示集合S,每一位代表一个元素。该位为0表示该元素在集合S中不存在,为1表示该元素在集合S中存在

位数 4 3 2 1

S     1 0 1 1

这个集合S里有元素1、2、4

以下是二进制数表示几种集合运算的方法

1、集合S的全集U=(1<<n)-1

2、检查集合S中是否含元素i   S&(1<<(i-1))   (返回0表示不存在。返回1表示存在)

3、从集合S中去除元素i S^(1<<(i-1))

以下是本题的思路:

首先对整个图跑一次Floyd多源最短路。得到两两点之间的最短距离,然后用DP求解,f[{S}][j]=已经訪问过的城市集合为S。訪问了j个城市,所需的最少花费。

f[S][i]=min{f[S-{j}][j]+dist[j][i]}

最后得到的答案ans=min(f[全集][i]+dist[i][0])

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm> #define MAXN 15
#define MAXM 1<<15
#define INF 0x3f3f3f3f using namespace std; int f[MAXM][MAXN]; //f[{S}][j]=已经訪问过的城市集合为S。訪问了j个城市,所需的最少花费
int dist[MAXN][MAXN]; //点与点之间的距离
int n; int min(int a,int b)
{
if(a<b) return a;
return b;
} void Floyd()
{
for(int k=0;k<=n;k++)
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
} int TSP() //DP求TSP
{
memset(f,0x7f,sizeof(f));
for(int s=0;s<(1<<n);s++) //枚举訪问城市集合S,全集为(1<<n)-1
for(int i=1;i<=n;i++) //枚举近期訪问过的城市i
if(s&(1<<(i-1))) //city(i)∈S
{
if(s==(1<<(i-1))) //{city(i)}==S
f[s][i]=dist[0][i];
else
{
for(int j=1;j<=n;j++) //枚举上一次訪问的城市j
if((s&(1<<(j-1)))&&i!=j) //城市j不和i同样
f[s][i]=min(f[s][i],f[s^(1<<(i-1))][j]+dist[j][i]); //Cs {city(J)}=s^(1<<(i-1))
}
}
int ans=INF;
for(int i=1;i<=n;i++)
ans=min(ans,f[(1<<n)-1][i]+dist[i][0]);
return ans;
} int main()
{
while(scanf("%d",&n)&&n)
{
memset(dist,0,sizeof(dist));
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
scanf("%d",&dist[i][j]);
Floyd();
printf("%d\n",TSP());
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

[POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法的更多相关文章

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

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

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

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

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

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

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

  6. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

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

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  8. POJ 3311 Hie with the Pie 【状压DP】

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

  9. poj 3311 Hie with the Pie

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

随机推荐

  1. hdu 5101 Select(Bestcoder Round #17)

    Select                                                    Time Limit: 4000/2000 MS (Java/Others)     ...

  2. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  3. lua-TestMore(转)

    http://fperrad.github.io/lua-TestMore/ http://www.softpedia.com/get/Programming/Debuggers-Decompiler ...

  4. lunux命令笔记

    文件查看命令 ls / -lh ls list / 路径 -l 具体 -lh 具体的人性化显示 -ld 显示文件夹 -i 显示i节点 mkdir /tmp/mulu/mulu2 /tmp/ma/mb ...

  5. Xaml于string(弦)定义常量和处理空间

    xml version="1.0" encoding="UTF-8"? > (1)基本使用方法 xaml中能够实例化各种对象,比方在ResourceDic ...

  6. [LeetCode66]Plus One

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  7. Unity判断网络连接类型

    使用NetworkReachability判断手机游戏当前的网络连接类型,是wifi还是234G using UnityEngine; using System.Collections; public ...

  8. 在SQL2008中,如何让id自动生成并自动递增?如何让时间默认生成?

    id自动递增:    如果是用语句操作,这样定义:ID INT IDENTITY,如果是要生成一对数字,这样定义:ID INT IDENTITY(1,1)    如果要在SQL Server的表中设置 ...

  9. ORACLE 实验一

    实验一:数据定义 实验学时:4学时 实验类型:综合型 实验要求:必修 一.实验目的 1.熟悉Oracle的client配置: 2.掌握SQL Plus的使用: 3.掌握SQL模式定义语句,定义相关的表 ...

  10. 在内存中建立 MySQL 的临时目录(转)

    MySQL 系统会在内存(MEMORY)和磁盘(MyISAM)中建立临时表,如何能知道在磁盘中建立了多少临时表以及在内存中建立多少临时表呢?你可以通过下面命令获知: ? 1 2 3 4 5 6 7 m ...