POJ 3311 Hie with the Pie (Floyd + 暴力全排列 || Floyd + 状态压缩DP (已补充))

来源:互联网 发布:淘宝不能好评返现了 编辑:程序博客网 时间:2024/06/06 19:39
Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 6782 Accepted: 3654

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 processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it means passing the same location(s) or the pizzeria more than once on the way. He has commissioned you to write a program to help him.

Input

Input will consist of multiple test cases. The first line will contain a single integern indicating the number of orders to deliver, where 1 ≤n ≤ 10. After this will ben + 1 lines each containingn + 1 integers indicating the times to travel between the pizzeria (numbered 0) and then locations (numbers 1 ton). Thejth value on the ith line indicates the time to go directly from locationi to locationj without visiting any other locations along the way. Note that there may be quicker ways to go fromi toj via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from locationi toj may not be the same as the time to go directly from locationj toi. An input value of n = 0 will terminate input.

Output

For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.

Sample Input

30 1 10 101 0 1 210 1 0 1010 2 10 00

Sample Output

8

Source

East Central North America 2006

大体题意:
你的位置在0号,你有n 个位置要送披萨,分别为1,2,3,,,n,求出一条道路来使得去和回来的路之和最小!
思路:
因为数据量太小了,所以可以随心所欲的暴力!
我们先用Floyd 求出任意两点的最短距离,然后他肯定有一个送披萨的顺序,我们可以对1,2,,,n进行全排列,然后枚举答案取最小值即可!
思路2:
状态压缩DP:
今天刚刚学了状态压缩DP,干脆拿这道题目练练,补充一下这篇博客!
可以枚举S,假设有n个地方的话,可以用n位二进制数来代表,  每一位i 如果是1 的话,表示当前状态访问过i地方,0代表没有访问到!这样的话 ,就可以用dp[s][i]表示状态s 到达i 的最短路!
这道题目他是可以有回头路的,回头路怎么解决,貌似很麻烦,其实用Floyd算法完美解决了这道题目回头路问题,比如说你最后求出的顺序是0-2-3吧,0-2 ,2-0,0-3 这个路径假设比 0-2,2-3快的话,那么Floyd求出的路径就是2-0.0-3,可以实现回头路!
整体过程就是:
我们枚举状态S,在枚举地方i,如果这个状态 只有i的话, 也就是S == 1<< (i-1)的话,那么dp[s][i] = dis[0][i],也就从0号直接到i!
如果不只有地方i 的话,那么我们可以找到一个中间状态S和中间位置j 使得S包括j 但不包括i
那么状态转移就是dp[s][i] = min(dp[s2][j] + dis[j][i],dp[s][i]);
好的,剩下的关键是如何找到状态s2,假设状态S包括i,也包括了j,那么s2 = S ^ (1 << (i-1))
有几个位运算技巧:
S  >> i & 1 表示 状态s 第i位是否为1 (i从0开始计算)
或者S& (1 << i)也表示第i位是否为1
S ^ (1 << i)表示求出一个新状态不包括i
最后只需要枚举终点i即可!
详细见代码:
状态压缩思路  AC代码:


==================================================
2016.10.24 21:41  再次补充:(记忆化搜索)
典型的TSP问题!
直接令dp[i][s]为当前在城市i   还需要访问集合s 中的城市一次 回到0 号节点的最小时间!
那么直接找到状态s 中的每一个1所在的位置 计算走到这个位置 即可!
注意  题目中说到   从i 走到j  无论怎样走都可以 那就暗示了 是Floyd 走法!!!!
dfs 转移下一个状态即可!

记忆花搜索代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 17;const int inf = 0x3f3f3f3f;int a[maxn][maxn];int dp[maxn][1024],n;int min(int& a,int b){    return a > b ? b : a;}int dfs(int k,int s){    int& ans = dp[k][s];    if (!s){        return ans = a[k][0];    }    if (~ans) return ans;    ans = inf;    for (int i = 0; i < n; ++i){        if (s & (1 << i)){            ans = min(ans,a[k][i+1] + dfs(i+1,s^(1 << i)));        }    }    return ans;}int main(){    while(scanf("%d",&n) == 1 && n){        memset(a,inf,sizeof a);        for (int i = 0; i <= n; ++i){            for (int j = 0; j <= n; ++j){                int t;                scanf("%d", &t);                a[i][j] = min(a[i][j],t);            }        }        for (int k = 0; k <= n; ++k)        for (int i = 0; i <= n; ++i){            for (int j = 0; j <= n; ++j){                a[i][j] = min(a[i][j],a[i][k] + a[k][j]);            }        }        memset(dp,-1,sizeof dp);        printf("%d\n",dfs(0,(1 << n) - 1));    }    return 0;}/**30 1 10 101 0 1 210 1 0 1010 2 10 0**/


=============================
递推代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 11;const int inf = 0x3f3f3f3f;int g[maxn][maxn];int dis[maxn][maxn];int dp[1<<maxn][maxn];int main(){    int n;    while(scanf("%d",&n) == 1 && n){        for (int i = 0; i <= n; ++i){            for (int j = 0; j <= n; ++j){                scanf("%d",&g[i][j]);            }        }        memset(dis,inf,sizeof dis);        for (int k = 0; k <= n; ++k){            for (int i = 0; i <= n; ++i){                for (int j = 0; j <= n; ++j){                    dis[i][j] = min(g[i][k]+g[k][j],dis[i][j]);                }            }        }        int len = (1 << n);        memset(dp,inf,sizeof dp);        for (int s = 1; s < len; ++s){            for (int i = 1; i <= n; ++i){                if (!(s>>(i-1)&1))continue; // 没有经过i号位置!                if (s == (1 << (i-1)) ){  // 只经过了i,距离相当于从0号直接到i位置!                    dp[s][i] = dis[0][i];                    continue;                }                for (int j = 1; j <= n; ++j){                    if (j == i)continue;//i 和 j 相同!                    if (!(s>>(j-1)&1))continue;  // S状态没有经过j 城市!                    dp[s][i] = min(dp[s][i],dp[s^(1<<(i-1))][j] + dis[j][i]);                }            }        }        int ans = inf;        for (int i = 1; i <= n; ++i){            ans = min(ans,dp[(1<<n)-1][i] + dis[i][0]);        }        printf("%d\n",ans);    }    return 0;}

暴力思路 AC代码!
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib>#include<vector>#include<iostream>#include<set>#include<map>#include<stack>#include<string>#include<queue>using namespace std;const double eps = 1e-10;const int inf = 0x3f3f3f3f;const double pi = acos(-1.0);typedef long long ll;typedef unsigned ULL;struct Point{    double x,y;    Point(double x = 0,double y = 0):x(x),y(y){}    bool operator < (const Point& rhs) const {        return x < rhs.x || (fabs(x- rhs.x) < eps && y < rhs.y);    }    bool operator == (const Point & rhs) const {        return fabs(x - rhs.x) < eps && fabs(y - rhs.y) < eps;    }};typedef Point Vector;Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y + B.y); }Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y - B.y); }Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); }Vector operator / (Vector A,double p) { return Vector(A.x/p,A.y/p); }double Cross(Vector A,Vector B) { return A.x*B.y - B.x*A.y; }Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){    Vector u = P-Q;    double t = Cross(w, u) / Cross(v, w);    return P + v*t;}int a[1007];char s[1007];int g[17][17];int g2[17][17];int pai[100];int main(){    int n;    while(scanf("%d",&n) == 1 && n){        for (int i = 1; i<= n; ++i)pai[i] = i;        for (int i = 0; i <= n; ++i){            for (int j = 0; j <= n; ++j){                scanf("%d",&g[i][j]);            }        }        memset(g2,inf,sizeof g2);        for (int k = 0; k <= n; ++k){            for (int i = 0; i <= n; ++i){                for (int j = 0; j <= n; ++j){                    g2[i][j] = min(g2[i][j],g[i][k] + g[k][j]);                }            }        }        ll Ans = inf;        do{            ll ans = 0;            int cur = 0;            for (int i = 1; i<= n; ++i){                ans += g2[cur][pai[i]];                cur = pai[i];            }            ans += g2[cur][0];            Ans = min(Ans,ans);//            printf("%d\n",ans);//            if (ans == 6)for (int i = 1; i <= n; ++i)printf("%d ",pai[i]);//            puts("");        } while(next_permutation(pai+1,pai+1+n));        printf("%lld\n",Ans);    }    return 0;}


0 0
原创粉丝点击