[POJ 3311]Hie with the Pie——再谈TSP问题的DP解法

题目连接:



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;
}
时间: 2024-08-09 10:44:55

[POJ 3311]Hie with the Pie——再谈TSP问题的DP解法的相关文章

POJ 3311 Hie with the Pie (状压DP)

状态压缩DP dp[i][j]表示在i状态(用二进制表示城市有没有经过)时最后到达j城市的最小时间 转移方程dp[i][j]=min(dp[i][k]+d[k][j],dp[i][j]) d[k][j]是k城市到j城市的最短距离 要先用flody处理 #include<bits.stdc++.h> using namespace std; int d[20][20],dp[1<<11][20]; int n,m; void flody() { for(int k=0;k<=n

poj 3311 Hie with the Pie 【旅行商+回原点】

题目:poj 3311 Hie with the Pie 题意:就是批萨点小二要送批萨,然后给你每个点的距离,有向的,然后让你就走一次回到原点的最短路. 分析:因为给出的是稠密图,所以要处理一下最短路,floyd 然后TSP就好. 枚举每个状态,对于当前状态的每一个已经走过的点,枚举是从那个点走过来的,更新最短路 状态:dp[st][i] :st状态下走到点 i 的最短路 转移方程:dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);

POJ 3311 Hie with the Pie TSP+Floyd

保证每个点访问过一次就行,然后会到原点. 这种情况可以先做一边floyd,然后跑tsp就好. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque&g

POJ 3311 Hie with the Pie (Floyd + 状压dp 简单TSP问题)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5019   Accepted: 2673 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

poj 3311 Hie with the Pie dp+状压

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

POJ 3311 Hie with the Pie

Hie with the Pie Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 331164-bit integer IO format: %lld      Java class name: Main The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast a

poj 3311 Hie with the Pie (状态压缩+最短路)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

状压DP [POJ 3311] Hie with the Pie

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4712   Accepted: 2502 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

POJ 3311 Hie with the Pie(状压dp or dfs)

Language: Default Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5057   Accepted: 2695 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutba