POJ 3311 Hie with the Pie floyd+状压DP

链接:http://poj.org/problem?id=3311

题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多少。

思路:首先用floyd找到全部点之间的最短路。然后用状态压缩,dp数组一定是二维的,假设是一维的话不能保证dp[i]->dp[j]一定是最短的。由于dp[i]记录的“当前位置”不一定是能使dp[j]最小的当前位置。所以dp[i][j]中,i表示的二进制下的当前已经经过的状态,j表示的是在当前状态下眼下所在的位置。

代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define PI acos(-1.0)
#define seed 31//131,1313
#define maxn 15
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int dp[1<<10][maxn];
int Pow[maxn];
int back[maxn];
int cost[maxn][maxn];
void init1()
{
    Pow[0]=1;
    for(int i=1; i<=10; i++)
        Pow[i]=Pow[i-1]*2;
}
void init2()
{
    for(int i=1; i<(1<<10); i++)
        for(int j=0; j<=10; j++)
            dp[i][j]=INF;
}
int floyd(int a[][maxn],int t)
{
    for(int i=0; i<t; i++)
        for(int j=0; j<t; j++)
            for(int k=0; k<t; k++)
                a[i][j]=min(a[i][k]+a[k][j],a[i][j]);
}
int main()
{
    int T,x;
    init1();
    while(scanf("%d",&T))
    {
        init2();
        if(!T)
            break;
        for(int i=0; i<T+1; i++)
            for(int j=0; j<T+1; j++)
                scanf("%d",&cost[i][j]);
        floyd(cost,T+1);

        for(int i=0; i<T; i++)
            dp[Pow[i]][i]=cost[0][i+1];
        for(int i=0; i<T; i++)
            back[i]=cost[i+1][0];
        for(int i=0; i<T; i++)
            for(int j=0; j<T; j++)
                cost[i][j]=cost[i+1][j+1];
        for(int i=0; i<(1<<T); i++)
        {
            if(i==1||i==2||i==4||i==8||i==16||i==32||i==64||i==128||i==256||i==512)
            continue;
            int ii=i;
            int pos=0;
            while(ii)
            {
                if(ii%2==1)
                {
                    int t=i-Pow[pos];
                    for(int j=0; j<T; j++)
                        if(dp[t][j]!=INF&&dp[t][j]+cost[j][pos]<dp[i][pos])
                            dp[i][pos]=dp[t][j]+cost[j][pos];
                }
                ii>>=1;
                pos++;
            }
        }
        int ans=INF;
        for(int i=0; i<T; i++)
        {
            if(dp[(1<<T)-1][i]!=INF&&dp[(1<<T)-1][i]+back[i]<ans)
                ans=dp[(1<<T)-1][i]+back[i];
        }

        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-03 20:49:39

POJ 3311 Hie with the Pie floyd+状压DP的相关文章

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)

题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORLD (可重复走的TSP问题,状压DP)这道题几乎一模一样. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #includ

POJ 3311 Hie with the Pie(状压DP + Floyd)

题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait fo

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

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

贴一个TSP讲解:点击打开链接 错误的转移方程 dp[i][j] 把i当作了步数,以为至多走N步就可以了.作死啊 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define maxn 1100 #define inf 0x3f3f3f3f const double eps=1e-8; using namespace std; int dp[12][1<

[POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法

主题连接:  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

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

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be

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 TSP+Floyd

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