Test instructions: There are different grades of pearls, the price is different, now list the need to buy pearls, to spend the least money (can buy high-grade to replace low-grade)
Analysis: Dp[i] represents the minimum amount of money to spend from the lowest level to the I level, dp[i]=min (Dp[j]+v) v is the money from j+1 to i all buy I grade flowers. Requires a triple loop.
Code:
#include <iostream> #include <algorithm> #include <cstdio> #define INF 1000000007using namespace std; int t,n;int a[2000],b[2000];int dp[2000];int min (int p,int q) {return p<q?p:q;} int DP () {for (Int. i=0;i<=n;i++) dp[i]=inf;dp[0]=0;for (int i=1;i<=n;i++) {for (int j=0;j<i;j++) {int v=0;for (int) k=j+1;k<=i;k++) v+=a[k]; v= (V+10) *b[i]; Dp[i]=min (Dp[j]+v,dp[i]);}} return dp[n];} int main () {cin>>t;while (t--) {cin>>n;for (int i=1;i<=n;i++) cin>>a[i]>>b[i];cout<< DP () <<endl;}}
HDU 1300 PEARLS--DP