****题(alb)

sol:较简单的dp题,n4随便写写,n3需要加一个小优化

int i,j,k,i1,j1,i2,j2;
        memset(dp,63,sizeof dp);
        for(i=0;i<n;i+=2) dp[1][i][i+1]=dp[1][i+1][i]=W[i][i+1];
        for(i=2;i<=m;i++)
        {
            int tmp=Bin[i-1];
            for(j=0;j<n;j+=Bin[i])
            {
                int l=j,r=j+Bin[i]-1,mid=l+tmp-1;
                for(i1=l;i1<=mid;i1++) for(j1=l;j1<=mid;j1++) if(i1!=j1)
                {
                    for(i2=mid+1;i2<=r;i2++) for(j2=mid+1;j2<=r;j2++) if(i2!=j2)
                    {
                        cmin(dp[i][i1][j2],dp[i-1][i1][j1]+dp[i-1][i2][j2]+W[j1][i2]);
                        cmin(dp[i][j2][i1],dp[i-1][j2][i2]+dp[i-1][j1][i1]+W[i2][j1]);
                    }
                }
            }
        }
        int ans=inf;
        for(i=0;i<Bin[m-1];i++)
        {
            for(j=Bin[m-1];j<n;j++)
            {
                ans=min(ans,dp[m][i][j]);
            }
        }
        Wl(ans);
n^4
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=1505,inf=0x3f3f3f3f;
int n,m,Bin[15];
int W[N][N];
int dp[15][N][N],f[N][N];
inline void cmin(int &x,int y) {x=min(x,y);}
namespace alb
{
    inline void Solve()
    {
        int i,j,k,i1,j1,i2,j2;
        memset(dp,63,sizeof dp);
        for(i=0;i<n;i+=2) dp[1][i][i+1]=dp[1][i+1][i]=W[i][i+1];
        for(i=2;i<=m;i++)
        {
            int tmp=Bin[i-1];
            for(j=0;j<n;j+=Bin[i])
            {
                int l=j,r=j+Bin[i]-1,mid=l+tmp-1;
                for(i1=l;i1<=mid;i1++) for(i2=mid+1;i2<=r;i2++) f[i1][i2]=inf;
                for(i1=l;i1<=mid;i1++) for(j1=l;j1<=mid;j1++) if(i1!=j1) for(i2=mid+1;i2<=r;i2++)
                {
                    cmin(f[i1][i2],dp[i-1][i1][j1]+W[j1][i2]);
                }
                for(i1=l;i1<=mid;i1++) for(i2=mid+1;i2<=r;i2++) for(j2=mid+1;j2<=r;j2++) if(i2!=j2)
                {
                    cmin(dp[i][i1][j2],f[i1][i2]+dp[i-1][i2][j2]);
                    dp[i][j2][i1]=dp[i][i1][j2];
                }
            }
        }
        int ans=inf;
        for(i=0;i<Bin[m-1];i++)
        {
            for(j=Bin[m-1];j<n;j++) ans=min(ans,dp[m][i][j]);
        }
        Wl(ans);
    }
}
int main()
{
    freopen("alb.in","r",stdin);
    freopen("alb.out","w",stdout);
    int i,j;
    Bin[0]=1; for(i=1;i<=10;i++) Bin[i]=Bin[i-1]<<1;
    R(m); n=Bin[m];
    for(i=0;i<n;i++) for(j=0;j<n;j++) R(W[i][j]);
    alb::Solve();
    return 0;
}
/*
input
2
0 7 2 1
7 0 4 3
2 4 0 5
1 3 5 0
output
13
*/
n^3

 

posted @ 2019-07-14 21:41  yccdu  阅读(140)  评论(0编辑  收藏  举报