Dynamic Planning Special - solving report

 

 

A shaped pressing the DP ,

Note To open long long

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[1<<18][18];
ll e[20][20];
ll x[20],y[20];
const ll INF=1e18;
int main()
{
    int n,s;
    scanf("%d%d",&n,&s);
    int tot=0;
    for(int i=1; i<=n; i++)
    {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        if(s==i)
        {
            x[n-1]=a;
            y[n-1]=b;
        }
        else
        {
            x[tot]=a;
            y[tot]=b;
            tot++;
        }
    }
    for(int i=0; i<tot; i++)
        for(int j=0; j<tot; j++)
        {
            e[i][j]=abs(x[i]-x[j])+abs(y[i]-y[j]);
        }
    for(int s=0;s<(1<<tot);s++)
        for(int i=0;i<tot;i++)
            dp[s][i]=INF;
    for(int i=0;i<tot;i++)
        dp[1<<i][i]=abs(x[i]-x[n-1])+abs(y[i]-y[n-1]);
    for(int s=0;s<(1<<tot);s++)
        for(int i=0;i<tot;i++)
            for(int j=0;j<tot;j++)
            {
                if(i!=j&&((1<<i)&s)&&(((1<<j)&s)==0))
                {
                    dp[s|1<<j][j]=min(dp[s|1<<j][j],dp[s][i]+e[i][j]);
                }
            }
    ll ans=INF;
    for(int i=0;i<tot;i++)
    {
        ans=min(ans,dp[(1<<tot)-1][i]);
//        printf("%d\n",dp[(1<<tot)-1][i]);
    }
    printf("%lld",ans);
}
View Code

 

Guess you like

Origin www.cnblogs.com/dongdong25800/p/11145088.html