poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 状压dp

题目链接

题意

给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点)。

思路

因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间的最短路径。

接下来就是状压dp的部分。

将已经经过的点的状态用\(state\)表示,

则\(dp[state][k]\)表示当前到达点\(k\)后状态为\(state\)时的最短路径长度。

\[ans=min_{i=1}^{n}(dp[(1<<n)-1][i]+dis[i][0])\]

可用记忆化搜索

Code

#include <cstdio>
#include <iostream>
#include <cstring>
#include <climits>
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
#define maxn 12
#define maxs 1100
using namespace std;
typedef long long LL;
int n, a[maxn][maxn], dp[maxs][maxn];
bool vis[maxs][maxn];
void floyd() {
    F2(k, 0, n) {
        F2(i, 0, n) {
            F2(j, 0, n) {
                if (i==j||i==k||j==k) continue;
                a[i][j] = min(a[i][j],a[i][k]+a[k][j]);
            }
        }
    }
}
int dfs(int state, int p) {
    if (state==(1<<(p-1))) return dp[state][p] = a[0][p];
    if (vis[state][p]) return dp[state][p];
    vis[state][p] = true;
    int ans = INT_MAX, sta = state&~(1<<(p-1));
    F2(i, 1, n) {
        if (state&(1<<(i-1)) && i!=p) {
            ans = min(ans, dfs(sta, i)+a[i][p]);
        }
    }
    return dp[state][p] = ans;
}
void work() {
    memset(dp, 0, sizeof dp);
    memset(vis, 0, sizeof vis);
    F2(i, 0, n) {
        F2(j, 0, n) {
            scanf("%d", &a[i][j]);
        }
    }
    floyd();
    int ans = INT_MAX;
    F2(i, 1, n) ans = min(ans, dfs((1<<n)-1, i)+a[i][0]);
    printf("%d\n", ans);
}
int main() {
    while (scanf("%d", &n) != EOF && n) work();
    return 0;
}

原文地址:https://www.cnblogs.com/kkkkahlua/p/8449095.html

时间: 2024-10-31 15:15:23

poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 状压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

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问题)

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