首页 > 代码库 > POJ #1260 - Pearls
POJ #1260 - Pearls
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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。