【BZOJ1560】[JSOI2009]火星藏宝图【DP】

【题目链接】

可以证明,如果有A->B->C,那么A->C一定不是最优的。然后搞一搞就行啦。

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 200005, maxm = 1005, inf = 0x3f3f3f3f;

int n, m, dp[maxn], now[maxm];

struct _point {
	int x, y, w;

	bool operator < (const _point &a) const {
		return y != a.y ? y < a.y : x < a.x;
	}
} p[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int dis(_point &a, _point &b) {
	return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int main() {
	n = iread(); m = iread();
	for(int i = 1; i <= n; i++) p[i].x = iread(), p[i].y = iread(), p[i].w = iread();

	sort(p + 1, p + 1 + n);
	memset(dp, -0x3f, sizeof(dp));

	dp[1] = p[1].w;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= p[i].x; j++) if(now[j])
			dp[i] = max(dp[i], dp[now[j]] - dis(p[now[j]], p[i]) + p[i].w);
		now[p[i].x] = i;
	}

	printf("%d\n", dp[n]);
	return 0;
}


你可能感兴趣的:(【BZOJ1560】[JSOI2009]火星藏宝图【DP】)