poj 3311 状压dp 最短路

C - Hie with the Pie
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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

3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

Sample Output

8

题意转自:http://blog.csdn.net/xymscau/article/details/6709588

题意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小。

最近有点sb,经常犯sb的错误,今天又是,蛋疼啊。这题很明显的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处理一下。

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<vector>
 10 
 11 #define N 105
 12 #define M 100000
 13 #define inf 1000000007
 14 #define mod 1000000007
 15 #define mod2 100000000
 16 #define ll long long
 17 #define maxi(a,b) (a)>(b)? (a) : (b)
 18 #define mini(a,b) (a)<(b)? (a) : (b)
 19 
 20 using namespace std;
 21 
 22 int n;
 23 int a[N][N];
 24 int d[N][N];
 25 int dp[ (1<<11)+10 ][N];
 26 int mi;
 27 
 28 void flody()
 29 {
 30     int i,j,k;
 31     for(k=0;k<n+1;k++){
 32         for(i=0;i<n+1;i++){
 33             for(j=0;j<n+1;j++){
 34                 d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
 35             }
 36         }
 37     }
 38 }
 39 
 40 void ini()
 41 {
 42     int i,j;
 43    // m=n+1;
 44     mi=inf;
 45     memset(dp,-1,sizeof(dp));
 46     for(i=0;i<=n;i++){
 47         for(j=0;j<=n;j++){
 48             scanf("%d",&a[i][j]);
 49             d[i][j]=a[i][j];
 50         }
 51     }
 52     flody();
 53 }
 54 
 55 
 56 
 57 void solve()
 58 {
 59     int o,j,p,te;
 60     dp[0][0]=0;
 61     for(o=0;o<(1<<n);o++){
 62         for(j=0;j<n;j++){
 63             if( ( (1<<j) & o)==0 ) continue;
 64             if( (1<<j)==o ){
 65                 dp[o][j+1]=d[0][j+1];continue;
 66             }
 67             dp[o][j+1]=inf;
 68             te=o ^ (1<<j);
 69             for(p=0;p<n;p++){
 70                 if( ( (1<<p) & te)==0 ) continue;
 71                 dp[o][j+1]=min(dp[o][j+1],dp[te][p+1]+d[p+1][j+1]);
 72             }
 73         }
 74     }
 75 
 76     for(j=0;j<n;j++){
 77         mi=min(mi,dp[ (1<<n)-1 ][j+1]+d[j+1][0]);
 78     }
 79 
 80     //for(o=0;o<(1<<n);o++){
 81     //    for(j=1;j<=n;j++) printf(" o=%d j=%d dp=%d
",o,j,dp[o][j]);
 82     //}
 83 
 84 }
 85 
 86 int main()
 87 {
 88    // freopen("data.in","r",stdin);
 89     //freopen("data.out","w",stdout);
 90     //scanf("%d",&T);
 91    // for(int cnt=1;cnt<=T;cnt++)
 92    // while(T--)
 93     while(scanf("%d",&n)!=EOF)
 94     {
 95         if(n==0) break;
 96         ini();
 97         solve();
 98         printf("%d
",mi);
 99     }
100     return 0;
101 }
原文地址:https://www.cnblogs.com/njczy2010/p/3947821.html