2000 Asia shanghai Dance Dance Revolution

思路:dp[i][x][y]表示第i个序列中,右脚在x位置,左脚在y位置时,其最小花费。

那么dp[i][x][y]=min(dp[i-1][a[i]][y]+cost[a[i]][x],dp[i-1][x][a[i]]+cost[a[i]][y]);

题目连接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=32

#include<map>

#include<set>

#include<cmath>

#include<queue>

#include<cstdio>

#include<vector>

#include<string>

#include<cstdlib>

#include<cstring>

#include<iostream>

#include<algorithm>

#define Maxn 2010

#define Maxm 100010

#define LL __int64

#define Abs(x) ((x)>0?(x):(-x))

#define lson(x) (x<<1)

#define rson(x) (x<<1|1)

#define inf 0x7fffffff

#define Mod 1000000007

using namespace std;

int cost[5][5]={{1,2,2,2,2},{2,1,3,4,3},{2,3,1,3,4},{2,4,3,1,3},{2,3,4,3,1}};

int dp[200010][5][5];

int list[200010];

int main()

{

    int n,i,j,cnt,num,x,y;

    cnt=0;

    while(scanf("%d",&list[++cnt]),list[cnt])

    {

        memset(dp,48,sizeof(dp));

        while(scanf("%d",&num)){

            if(num!=0) {

                list[++cnt]=num;

                continue;

            }

        dp[1][list[1]][0]=cost[0][list[1]];

        dp[1][0][list[1]]=cost[0][list[1]];

        for(i=2;i<=cnt;i++){

            for(x=0;x<=4;x++){

                for(y=0;y<=4;y++){

                    dp[i][list[i]][y]=min(dp[i][list[i]][y],dp[i-1][x][y]+cost[x][list[i]]);

                    dp[i][x][list[i]]=min(dp[i][x][list[i]],dp[i-1][x][y]+cost[y][list[i]]);

                }

            }

        }

        int ans=inf;

        for(i=1;i<=4;i++)

            for(j=1;j<=4;j++)

            ans=min(ans,dp[cnt][i][j]);

            printf("%d\n",ans);

            cnt=0;

            break;

        }

    }

    return 0;

}

 

你可能感兴趣的:(sha)