标签:style blog http color os art
DP is, enumeration + pruning. If you can think of enumeration pattern of a problem, plus a recurrence relation later, you are there.
Main ref: http://blog.csdn.net/kindlucy/article/details/5761795
The basic scenario is non-optimized case; all other possible cases, are optimized, and we try each solution of that.
// 1260 - Pearls // http://blog.csdn.net/kindlucy/article/details/5761795 // #include <stdio.h> #define MAX_CAT 100 #define Min(a,b) (a) < (b) ? (a) : (b) int calc(int cnt, int ai[MAX_CAT], int pi[MAX_CAT]) { int sum[MAX_CAT] = { 0 }; for (int i = 1; i <= cnt; i++) { sum[i] = sum[i - 1] + ai[i - 1]; } int dp[MAX_CAT + 1] = { 0 }; for (int i = 1; i <= cnt; i++) // dp[i] { int currmin = (ai[i - 1] + 10) * pi[i - 1] + dp[i - 1]; // non-optimized for (int j = 0; j < i; j ++) // try each replacement solution { int currtry = dp[j] + (sum[i] - sum[j] + 10) * pi[i - 1]; currmin = Min(currmin, currtry); } dp[i] = currmin; } return dp[cnt]; } int main() { int n; scanf("%d", &n); while (n--) { int ai[MAX_CAT] = { 0 }; int pi[MAX_CAT] = { 0 }; int cnt; scanf("%d", &cnt); for (int i = 0; i < cnt; i++) { scanf("%d%d", ai + i, pi + i); } int ret = calc(cnt, ai, pi); printf("%d\n", ret); } return 0; }
POJ #1260 - Pearls,布布扣,bubuko.com
标签:style blog http color os art
原文地址:http://www.cnblogs.com/tonix/p/3818256.html