poj 1695

用动态规划,dp[a][b][c]表示从位置最大的车在a(注意不是第一辆车),第二的车在b,第三的车在c开始最少需要的时间。

方程:dp[a][b][c]=max{dp[a+1][b][c],     //a到a+1

                   dp[a+1][a][c],      //b到a+1

                   dp[a+1][a][b],      //c到a+1

用滚动数组优化时空,status应该能到90左右

代码:

#include<cstdio>

#include<cstring>

using namespace std;

 

int dp[30][30]={0},d[30][30];

int min(int x,int y){

         return (x<y)?x:y;

}

int main(){

         int t;

         scanf("%d",&t);

         while(t--){

                   for(int i=0;i<30;i++)

                            for(int j=0;j<30;j++)

                                     dp[i][j]=0;

                   int n;

                   scanf("%d",&n);

                   for(int i=0;i<n-1;i++)

                            for(int j=i+1;j<n;j++)

                                     scanf("%d",&d[i][j]);

                   for(int a=n-2;a>=0;a--){

                            for(int b=0;b<=a;b++)

                                     for(int c=0;c<=a;c++){

                                               int ans=dp[b][c]+d[a][a+1];

                                               ans=min(ans,dp[a][c]+d[b][a+1]);

                                               ans=min(ans,dp[a][b]+d[c][a+1]);

                                               dp[b][c]=ans;

                                     }

                   }

                   printf("%d\n",dp[0][0]);

         }

         return 0;

}

posted @ 2013-07-07 15:19  shanquan2  阅读(173)  评论(0编辑  收藏  举报