其他分享
首页 > 其他分享> > 补给【蓝桥杯 第十一届】【决赛】【A组】(压装DP+最短路+dp顺序和平行更新的处理)

补给【蓝桥杯 第十一届】【决赛】【A组】(压装DP+最短路+dp顺序和平行更新的处理)

作者:互联网

题目:“蓝桥杯”练习系统 (lanqiao.cn)

思路:

#include <bits/stdc++.h>
using namespace std;
#define ri register int 
#define M 21

int n,m;
double dp[M][1<<21];
struct dian{
    int x,y;
}pp[M];
struct node{
    int to;
    double val;
};
vector <node> p[M];
double get(int a,int b)
{
    return sqrt((pp[a].x-pp[b].x)*(pp[a].x-pp[b].x)+(pp[a].y-pp[b].y)*(pp[a].y-pp[b].y));
}
double dis[M];
struct cmp{
    bool operator ()(const int a,const int b)const
    {
        return dis[a]>dis[b];
    }
};
int vis[M];
void dj(int a)
{
   for(ri i=1;i<=n;i++) dis[i]=1e9;
   dis[1]=0; 
   priority_queue<int,vector<int>,cmp> q;
   q.push(1);
   while(!q.empty())
   {
        while(!q.empty()&&vis[q.top()]) q.pop();
        if(q.empty()) break;
        int a=q.top();q.pop();
        vis[a]=1;
        for(ri i=0;i<p[a].size();i++)
        {
            int b=p[a][i].to;
            if(dis[b]>dis[a]+p[a][i].val)
            {
                dis[b]=dis[a]+p[a][i].val;
                q.push(b);
        }
    }
   }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin>>n>>m;
    for(ri i=1;i<=n;i++)
    {
      cin>>pp[i].x>>pp[i].y;    
    }
    for(ri i=1;i<=n;i++)
    {
        for(ri j=i+1;j<=n;j++)
        {
            if(get(i,j)<=m)
            {
                node t;
                t.to=j;t.val=get(i,j);
                p[i].push_back(t);
                t.to=i;
                p[j].push_back(t);
            }
        }
    }
    for(ri i=1;i<(1<<n);i++)
    {
        for(ri j=1;j<=n;j++)
        {
            dp[j][i]=1e9;
        }
    }
    dp[1][1]=0;
    for(ri i=1;i<(1<<n);i++)
    {
        for(ri j=1;j<=n;j++)
        {
            if((i&(1<<(j-1)))==0) continue;
            for(ri k=0;k<p[j].size();k++)
            {
                int b=p[j][k].to;
                if((i&(1<<(b-1)))==0) continue;
                dp[j][i]=min(dp[j][i],dp[b][i^((1<<(j-1)))]+p[j][k].val);
                dp[j][i]=min(dp[j][i],dp[b][i]+p[j][k].val);
            }
        }
        for(ri j=n;j>=1;j--)
        {
            if((i&(1<<(j-1)))==0) continue;
            for(ri k=0;k<p[j].size();k++)
            {
                int b=p[j][k].to;
                if((i&(1<<(b-1)))==0) continue;
                dp[j][i]=min(dp[j][i],dp[b][i^((1<<(j-1)))]+p[j][k].val);
                dp[j][i]=min(dp[j][i],dp[b][i]+p[j][k].val);
            }
        }
    }
    dj(1);
    double ans=1e9;
    for(ri i=1;i<=n;i++)
    {
        //cout<<dp[i][(1<<n)-1]+dis[i]<<endl;
        ans=min(ans,dp[i][(1<<n)-1]+dis[i]);
    }
    cout<<fixed<<setprecision(2)<<ans;
    
} 
View Code

后记:

标签:pp,压装,int,蓝桥,ri,dp,dis
来源: https://www.cnblogs.com/Lamboofhome/p/16375479.html