POJ 3311 Hie with the Pie (状态压缩DP ,TSP问题)

来源:互联网 发布:如何做免费网络推广 编辑:程序博客网 时间:2024/05/16 14:19

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 integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and the n locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to location j without visiting any other locations along the way. Note that there may be quicker ways to go from i to j 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 location i to j may not be the same as the time to go directly from location j to i. 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

题意;一个人需要去访问所有城镇然后返回原点的最短路问题。每个城镇可以多次访问

分析:比TSP问题多了多次访问这个条件,这个条件我们可以转化成,先求任意两点最短路,之后再问最短路,就成了TSP问题了。TSP问题解法很多,这里用状态压缩DP来解决。

状态DP【i】【j】表示 i 的二进制状态表示遍历的城镇,最后遍历城镇是第j个城镇。

DP【i】【j】=DP【i ^ j】【k】+mapp【k】【j】(K为(i^j)中遍历过的城镇)

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <queue>#define mem(p,k) memset(p,k,sizeof(p));#define rep(a,b,c) for(int a=b;a<c;a++)#define pb push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define inf 0x6fffffff#define ll long longusing namespace std;int mapp[20][20],dp[2000][20];void floyd(int n){    for(int k=0;k<=n;k++){        for(int i=0;i<=n;i++){            for(int j=0;j<=n;j++){                mapp[i][j]=min(mapp[i][j],mapp[i][k]+mapp[k][j]);            }        }    }}int count_1(int i){    int sum=0;    while(i){        if(i&1)sum++;        i>>=1;    }    return sum;}int main(){    int n;    while(scanf("%d",&n) && n){        for(int i=0;i<=n;i++){            for(int j=0;j<=n;j++)scanf("%d",mapp[i]+j);        }        floyd(n);        int tol=1<<n;        for(int i=0;i<tol;i++){            for(int j=0;j<n;j++){                if(i==(1<<j)){                    dp[i][j+1]=mapp[0][j+1];                }                else{                    dp[i][j+1]=inf;                    if(i & (1<<j)){                        for(int k=0;k<n;k++){                            if(i & (1<<k)){                                dp[i][j+1]=min(dp[i][j+1],dp[i ^(1<<j) ][k+1] + mapp[k+1][j+1]);                            }                        }                    }                }            }        }        int minn=inf;        for(int i=1;i<=n;i++)minn=min(minn,dp[tol-1][i]+mapp[i][0]);        cout<<minn<<endl;    }}




原创粉丝点击