題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6024
輸入不是按座標從小到大輸入的,先需要按座標從小到大排序
dp[i][j]表示前i個教室,最右邊糖果店在第j個教室時的花費
狀態轉移方程:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
static const int MAX_N = 3e3 + 5;
static const ll Mod = 1e9 + 7;
static const ll INF = (ll)1 << 60;
struct Node{
int x, c;
bool operator < (const Node &rhs) const {
return x < rhs.x;
}
}a[MAX_N];
ll dp[MAX_N][MAX_N];
int main() {
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) scanf("%d%d", &a[i].x, &a[i].c);
sort(a, a + n);
dp[0][0] = a[0].c;
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) dp[i][j] = dp[i - 1][j] + a[i].x - a[j].x;
dp[i][i] = INF;
for (int j = 0; j < i; ++j) dp[i][i] = min(dp[i][i], dp[i - 1][j] + a[i].c);
}
ll min_v = INF;
for (int i = 0; i < n; ++i) min_v = min(min_v, dp[n - 1][i]);
printf("%lld\n", min_v);
}
return 0;
}