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]);

然后最后在加上回去的距离即可。注意要更新和,这里wa了一次。

AC代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 12;
int mp[N][N];
int n;
int dp[1<<N][N];
int main()
{
    //freopen("Input.txt","r",stdin);
    while(~scanf("%d",&n) && n)
    {
        n++;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                scanf("%d",&mp[i][j]);
        }
        for(int k=0;k<n;k++)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                 mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
            }
        }
        for(int st=0;st<(1<<n);st++)
        {
            for(int i=0;i<n;i++)
            {
                if((st&(1<<i))==0)  //为0
                    continue;
                if(st==(1<<i)){
                    dp[st][i]=mp[0][i];continue;
                }
                dp[st][i]=inf;
                for(int j=0;j<n;j++)
                {
                    if((st&(1<<j)) && i!=j)//为1
                    {
                        dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);
                    }
                }
            }
        }
        int ans=inf;
        for(int i=0;i<n;i++){
            ans=min(ans,dp[(1<<n)-1][i]+mp[i][0]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-06 14:01:53

poj 3311 Hie with the Pie 【旅行商+回原点】的相关文章

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

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

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(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 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: 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