POJ 1157 LITTLE SHOP OF FLOWERS(动态规划)

//动态规划 //设S[i,k]表示第i种花束摆在第k个之前(包括第k个)的任意某个花瓶中,前i种花束能够获得的最大美学值(之和) //原问题的最优值即为S[F,V] //S[i,k] = max{S[i-1,k-1]+A(i,k),S[i,k-1]},(i>1,k>i); //初始条件为: //S[1,1] = A[1,1]; //S[1,k] = max{A(1,k),S[1,k-1]},(k>1); //S[i,i] = S[i-1,i-1]+A(i,i), (i>1) //要点,注意边界! #include<iostream> using namespace std; int F,V; int A[105][105],S[105][105]; int main() { scanf("%d%d",&F,&V); for(int i = 1;i <= F;++i) for(int j = 1;j <= V;++j) scanf("%d",&A[i][j]); S[1][1] = A[1][1]; for(int i = 2;i <= V;++i) S[1][i] = max(A[1][i],S[1][i-1]); for(int i = 2;i <= F;++i) S[i][i] = S[i-1][i-1] + A[i][i]; for(int i = 2;i <= F;++i) for(int j = i;j <= V;++j) S[i][j] = max(S[i-1][j-1] + A[i][j],S[i][j-1]); printf("%d/n",S[F][V]); } 

你可能感兴趣的:(POJ 1157 LITTLE SHOP OF FLOWERS(动态规划))