poj 3311(DP + 状态压缩)

Hie with the Pie

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5205   Accepted: 2790

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题目描述:给定一系列点,求从0点出发,走完其它点,可以重复走,回到0点的最短路程.假设n==4,那么点为0,1,2,3;路径可能为0 1 2 3 0        0 3 2 1 0        0 3 1 2 0     。。。。。。。 等等,其实就是1到n-1的排列组合,每次有n-1中选择,需要算出每种状态的值,找到最小值,我们可以设到达j点,已经经过了s,s表示经过点的集合。采用顺推的写法 dp[s | (1 <<k) ][k]=min(dp[s][j]+d[j][k],dp[s | (1<<k)][k]);d数组为两点之间的最短路.
#include <stdio.h>
#include <cstring >
#include <cstdlib>
#include <iostream>
#define maxn 12
#define inf  0x3f3f3f3f
using namespace std;

int n;
int a[maxn][maxn];
int d[maxn][maxn];
int dp[1<<maxn][maxn];
inline void  init()
{
   memset(d,inf,sizeof(d));
}
void floyed()
{
    for(int k=0;k<n;k++)
     for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
    {
      d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    }
}
void solve()
{
   /* for(int j=1;j<n;j++)
    {
       //dp[0][j]=d[0][j];
       dp[1][j]=d[0][j];

    }*/
    memset(dp,-1,sizeof(dp));
    dp[1][0]=0;
    for(int s=1;s< (1 << n);s++)
      for(int j=0;j<n;j++)
      {
          if( dp[s][j]!=-1)
          {
              for(int k=0;k<n;k++)
              {
                if( (k!=j))
                {
                    if(dp[s | (1<<k)][k]==-1 || (dp[s |(1<<k)][k] > (dp[s][j]+d[j][k])))
                        dp[s | (1 << k)][k]=dp[s][j]+d[j][k];
                   // cout<<dp[s | (1<<k )][k]<<endl;
                }

              }
          }
      }

    int ans=inf;
    int s=(1<<n)-1;
    /*for(int j=1;j<n;j++)
     {
         ans=min(ans,dp[s][j]+d[j][0]);
         printf("%d\n",dp[s][j]+d[j][0]);

     }*/
     printf("%d\n",dp[s][0]);

}
int main()
{
    //cout<<(1<<2)<<endl;
  // freopen("test.txt","r",stdin);
   while(~scanf("%d",&n) && n!=0)
   {
           init();
           n++;
       for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
      {
          scanf("%d",&a[i][j]);
          d[i][j]=a[i][j];
      }
      floyed();
      solve();
   }
    return 0;
}
时间: 2024-10-30 20:48:50

poj 3311(DP + 状态压缩)的相关文章

POJ 3311 【状态压缩DP】

题意: 给n个点,给出矩阵代表i到j单向边的距离. 要求,不介意访问每个点的次数,要求访问完每个点,使得路程总和最小. 思路: 由于不介意访问每个点的次数,所以可以先进行FLOYD求出任意两个点之间的最短路,然后就是DP. 同样的,1代表有访问过,0代表没访问过. dp[s][j]代表访问状态为s的情况下最终到达点j的最优值. 枚举状态s中每一个可以到达的点,从所有可能的点中枚举对其进行更新. 关于状态压缩的细节方面,注意从左边数第i位是从0开始数的,所以第i位代表第i+1个点. 然后状态的总数

Mondriaan&#39;s Dream - POJ 2411(状态压缩)

题目大意:有一些1*2的矩形,现在用这些小矩形覆盖M*N的大矩形,不能重复覆盖,并且要覆盖完全,求有多少种覆盖方式. 分析:可以使用1和0两种状态来表示这个位置有没有放置,1表示放置,0表示没有放置,可以有三种放置方式. 一,竖着放. 二,不放.三,横着放.直接DFS这些情况就行了................还是递归容易理解. 代码如下: =============================================================================

dp状态压缩

dp状态压缩 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的就是那种状态很多,不容易用一般的方法表示的动态规划问题,这个就更加的难于把握了.难点在于以下几个方面:状态怎么压缩?压缩后怎么表示?怎么转移?是否具有最优子结构?是否满足后效性?涉及到一些位运算的操作,虽然比较抽象,但本质还是动态规划.找准动态规划几个方面的问题,深刻理解动态规划的原理,开动脑筋思考问题.这才是掌握动态规划的关键. 动态规划最关键的要处理的问题就是位运算的操作,容易出错,状态的设计也直

HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由于得到每张卡片的状态不知道,所以用状态压缩,dp[i] 表示这个状态时,要全部收齐卡片的期望. 由于有可能是什么也没有,所以我们要特殊判断一下.然后就和剩下的就简单了. 另一个方法就是状态压缩+容斥,同样每个状态表示收集的状态,由于每张卡都是独立,所以,每个卡片的期望就是1.0/p,然后要做的就是要去重,既然

uva 11367 dijkstra+dp状态压缩

题意:给出n个地点 和 每个地点的油价 ,有 m 条边 , 并给出每条边长度 .1单位汽油可以走1千米  , 油箱的容量为 c , 在初始点 s 时 , 油箱中的油为 0 , 求s 到 t 的最小花费 . 解法: 定义 状态 d[i][j] 表示到达 地点 i 且油箱中有 j 单位油时的最小 花费. 对于状态的转移时 , 有两种方法: 1.把每个点的所有状态都求出 2.不把每个点的状态都求出 , 而是一单位一单位的加油. 对于第一种方法 , 会超时 , 因为每个点的状态太多 , 但是能用的状态就

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

HDU4352XHXJ&#39;s LIS(dp状态压缩)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 809    Accepted Submission(s): 308 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

Uva 10817 Headmaster&#39;s Headache (DP+ 状态压缩)

Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or