HDU6024 Building Shops(DP)

来源:互联网 发布:maven 打包 java 工程 编辑:程序博客网 时间:2024/06/08 05:03

题目链接

题意:

要在N个教室中的一些建造糖果屋,花费为在该教室建造的花费+其右侧未建造糖果的教室到该教室的距离

分析:

直接DP,d[i]表示在该点建造糖果屋的情况下[i..n]所有教室的最小花费

代码:

#define inf 0x3f3f3f3f3f3f3f3f#define maxn 3007ll n, a[maxn], c[maxn];ll b[maxn], d[maxn];ll work(int x) {    if (d[x] != -1) return d[x];    if (x == n) return d[x] = 0;    d[x] = inf;    ll cost = b[c[x]];    for (int j = x + 1; j <= n; j++) {        d[x] = min(d[x], work(j) + cost);        cost += 0ll + a[c[j]] - a[c[x]];    }    return d[x];}int cmp(int i, int j) {    return a[i] < a[j];}int main(){    while (~scanf("%lld", &n)) {        for (int i = 0; i < n; i++) {            scanf("%lld%lld", &a[i], &b[i]);            c[i] = i;        }        memset(d, -1, sizeof d);        sort(c, c + n, cmp);        work(0);        printf("%lld\n", d[0]);    }}