【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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值