zoukankan      html  css  js  c++  java
  • hlg 2130 状压dp

    基本的状压dp 需要注意的是两点之间直线最短 所以不需要进行floyd 

    由于把dp的memset放在了初始化0的后面de了好久的bug..

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<math.h>
    #include<map>
    using namespace std;
    #define eqs 0.000000001
    struct node
    {
        double x;
        double y;
    };
    node a[20];
    double f[20][20];
    double dp[1<<17][20];
    int main(){
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(f,0,sizeof(f));
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        for(int i=0;i<n;i++)
        {
            for(int k=0;k<n;k++)
            {
                 f[i][k]=f[k][i]=sqrt((a[i].x-a[k].x)*(a[i].x-a[k].x)+(a[i].y-a[k].y)*(a[i].y-a[k].y));
    
            }
        }
        int l=(1<<n)-1;
        memset(dp,0x7f,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            dp[1<<i][i]=0;
        }
    
        for(int s=0;s<=l;s++)
        {
            for(int i=0;i<n;i++)
            {
                if((s&(1<<i))==0)
                continue;
                for(int k=0;k<n;k++)
                {
                    if((s&(1<<k))!=0)
                        continue;
                    dp[s+(1<<k)][k]=min(dp[s+(1<<k)][k],dp[s][i]+f[i][k]);
                }
    
            }
        }
        double ans=1e300;
        for(int i=0;i<n;i++)
       {
           ans=min(dp[l][i],ans);
       }
       printf("%.2f
    ",ans);
    }
    }
    

      

  • 相关阅读:
    NOJ 1116 哈罗哈的大披萨 【淡蓝】 状态压缩DP
    优先队列原理与实现【转】
    testC-I
    济南NOIP冬令营 选拔(select)
    P4747 D’s problem(d)
    P4746 C’s problem(c)
    P4745 B’s problem(b)
    P4744 A’s problem(a)
    [bzoj] 1004: [HNOI2008]Cards
    NOIP2013 表达式求值
  • 原文地址:https://www.cnblogs.com/rayrayrainrain/p/5406429.html
Copyright © 2011-2022 走看看